13 * |
13 * |
14 * Description: Calendar Lunar Plugin |
14 * Description: Calendar Lunar Plugin |
15 * |
15 * |
16 */ |
16 */ |
17 |
17 |
18 #include <qstringlist.h> |
18 |
19 #include <hbglobal.h> |
19 #include "calendarui_debug.h" |
20 |
20 #include "calenextrarowformatter.h" |
|
21 #include <calenregionalutil.rsg> |
|
22 #include <AknBidiTextUtils.h> |
21 #include <badesca.h> |
23 #include <badesca.h> |
22 #include <eikenv.h> |
24 #include <eikenv.h> |
23 |
25 #include <StringLoader.h> |
24 #include "calendarui_debug.h" |
26 |
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 ); |
|
33 |
27 |
34 // ----------------------------------------------------------------------------- |
28 // ----------------------------------------------------------------------------- |
35 // CollapseDuplicatesL |
29 // CollapseDuplicatesL |
36 // ----------------------------------------------------------------------------- |
30 // ----------------------------------------------------------------------------- |
37 // |
31 // |
38 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub ) |
32 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub ) |
39 { |
33 { |
40 OstTraceFunctionEntry0( _COLLAPSEDUPLICATESL_ENTRY ); |
34 TRACE_ENTRY_POINT; |
|
35 |
41 const TInt sublen = aSub.Length(); |
36 const TInt sublen = aSub.Length(); |
42 if (aStr.Length() == KZero || sublen == KZero || aPos < KZero || (aPos >aStr.Length())) |
37 if (aStr.Length() == 0 || sublen == 0) |
43 { |
38 { |
44 OstTraceFunctionExit0( _COLLAPSEDUPLICATESL_EXIT ); |
|
45 return; |
39 return; |
46 } |
40 } |
47 |
41 |
48 TPtrC remaining = aStr.Mid( aPos ); |
42 TPtrC remaining = aStr.Mid( aPos ); |
49 TInt fstInRemaining = remaining.Find( aSub ); |
43 TInt fstInRemaining = remaining.Find( aSub ); |
50 |
44 |
51 if ( fstInRemaining >= KZero ) |
45 if ( fstInRemaining >= 0 ) |
52 { |
46 { |
53 TInt restPos = fstInRemaining + sublen; |
47 TInt restPos = fstInRemaining + sublen; |
54 TPtrC rest = remaining.Mid( restPos ); |
48 TPtrC rest = remaining.Mid( restPos ); |
55 TInt sndInRest = rest.Find( aSub ); |
49 TInt sndInRest = rest.Find( aSub ); |
56 |
50 |
57 // 1) two substrings found in sequence |
51 // 1) two substrings found in sequence |
58 if (sndInRest == KZero) |
52 if (sndInRest == 0) |
59 { |
53 { |
60 // replace second substring with empty string |
54 // replace second substring with empty string |
61 TInt fst = aPos + fstInRemaining; |
55 TInt fst = aPos + fstInRemaining; |
62 TInt snd = aPos + restPos + sndInRest; |
56 TInt snd = aPos + restPos + sndInRest; |
63 aStr.Replace( snd, sublen, KNullDesC); |
57 aStr.Replace( snd, sublen, KNullDesC); |
64 // continue collapsing from first |
58 // continue collapsing from first |
65 CollapseDuplicatesL( aStr, fst, aSub ); |
59 CollapseDuplicatesL( aStr, fst, aSub ); |
66 } |
60 } |
67 // 2) substring found later in string |
61 // 2) substring found later in string |
68 else if (sndInRest > KZero) |
62 else if (sndInRest > 0) |
69 { |
63 { |
70 // continue collapsing from this second substring |
64 // continue collapsing from this second substring |
71 TInt snd = aPos + restPos + sndInRest; |
65 TInt snd = aPos + restPos + sndInRest; |
72 CollapseDuplicatesL( aStr, snd, aSub ); |
66 CollapseDuplicatesL( aStr, snd, aSub ); |
73 } |
67 } |
74 // 3) No second substring found -> nothing to collapse |
68 // 3) No second substring found -> nothing to collapse |
75 else |
69 else |
76 { |
70 { |
77 OstTraceFunctionExit0( DUP1__COLLAPSEDUPLICATESL_EXIT ); |
71 TRACE_EXIT_POINT; |
78 return; |
72 return; |
79 } |
73 } |
80 } |
74 } |
81 // No substring found |
75 // No substring found |
82 else |
76 else |
83 { |
77 { |
84 OstTraceFunctionExit0( DUP2__COLLAPSEDUPLICATESL_EXIT ); |
78 TRACE_EXIT_POINT; |
85 return; |
79 return; |
86 } |
80 } |
87 OstTraceFunctionExit0( DUP3__COLLAPSEDUPLICATESL_EXIT ); |
|
88 } |
81 } |
89 |
82 |
90 // ----------------------------------------------------------------------------- |
83 // ----------------------------------------------------------------------------- |
91 // RemoveLeadingAndTrailingL |
84 // RemoveLeadingAndTrailingL |
92 // ----------------------------------------------------------------------------- |
85 // ----------------------------------------------------------------------------- |
93 // |
86 // |
94 void RemoveLeadingAndTrailingL( TDes& aStr, const TDesC& aSub ) |
87 void RemoveLeadingAndTrailingL( TDes& aStr, const TDesC& aSub ) |
95 { |
88 { |
96 OstTraceFunctionEntry0( _REMOVELEADINGANDTRAILINGL_ENTRY ); |
89 TRACE_ENTRY_POINT; |
|
90 |
97 // Trailing |
91 // Trailing |
98 const TInt sublen = aSub.Length(); |
92 const TInt sublen = aSub.Length(); |
99 if ( aStr.Right( sublen ).Find( aSub ) == KZero ) |
93 if ( aStr.Right( sublen ).Find( aSub ) == 0 ) |
100 { |
94 { |
101 aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC ); |
95 aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC ); |
102 } |
96 } |
103 |
97 |
104 // Leading |
98 // Leading |
105 if ( aStr.Left( sublen ).Find( aSub ) == KZero ) |
99 if ( aStr.Left( sublen ).Find( aSub ) == 0 ) |
106 { |
100 { |
107 aStr.Replace( KZero, sublen, KNullDesC ); |
101 aStr.Replace( 0, sublen, KNullDesC ); |
108 } |
102 } |
109 |
103 |
110 OstTraceFunctionExit0( _REMOVELEADINGANDTRAILINGL_EXIT ); |
104 TRACE_EXIT_POINT; |
111 } |
105 } |
112 |
106 |
113 // ----------------------------------------------------------------------------- |
107 // ----------------------------------------------------------------------------- |
114 // CCalenExtraRowFormatter::NewL |
108 // CCalenExtraRowFormatter::NewL |
115 // ----------------------------------------------------------------------------- |
109 // ----------------------------------------------------------------------------- |
116 // |
110 // |
117 EXPORT_C CCalenExtraRowFormatter* CCalenExtraRowFormatter::NewL() |
111 EXPORT_C CCalenExtraRowFormatter* CCalenExtraRowFormatter::NewL() |
118 { |
112 { |
119 OstTraceFunctionEntry0( CCALENEXTRAROWFORMATTER_NEWL_ENTRY ); |
113 TRACE_ENTRY_POINT; |
|
114 |
120 CCalenExtraRowFormatter* self = new (ELeave) CCalenExtraRowFormatter; |
115 CCalenExtraRowFormatter* self = new (ELeave) CCalenExtraRowFormatter; |
121 CleanupStack::PushL(self); |
116 CleanupStack::PushL(self); |
122 self->ConstructL(); |
117 self->ConstructL(); |
123 CleanupStack::Pop(self); |
118 CleanupStack::Pop(self); |
124 |
119 |
125 OstTraceFunctionExit0( CCALENEXTRAROWFORMATTER_NEWL_EXIT ); |
120 TRACE_EXIT_POINT; |
126 return self; |
121 return self; |
127 } |
122 } |
128 |
123 |
129 // ----------------------------------------------------------------------------- |
124 // ----------------------------------------------------------------------------- |
130 // CCalenExtraRowFormatter::~CCalenExtraRowFormatter |
125 // CCalenExtraRowFormatter::~CCalenExtraRowFormatter |
131 // ----------------------------------------------------------------------------- |
126 // ----------------------------------------------------------------------------- |
132 // |
127 // |
133 EXPORT_C CCalenExtraRowFormatter::~CCalenExtraRowFormatter() |
128 EXPORT_C CCalenExtraRowFormatter::~CCalenExtraRowFormatter() |
134 { |
129 { |
135 OstTraceFunctionEntry0( CCALENEXTRAROWFORMATTER_CCALENEXTRAROWFORMATTER_ENTRY ); |
130 TRACE_ENTRY_POINT; |
136 OstTraceFunctionExit0( CCALENEXTRAROWFORMATTER_CCALENEXTRAROWFORMATTER_EXIT ); |
131 TRACE_EXIT_POINT; |
137 } |
132 } |
138 |
133 |
139 // ----------------------------------------------------------------------------- |
134 // ----------------------------------------------------------------------------- |
140 // CCalenExtraRowFormatter::CCalenExtraRowFormatter |
135 // CCalenExtraRowFormatter::CCalenExtraRowFormatter |
141 // ----------------------------------------------------------------------------- |
136 // ----------------------------------------------------------------------------- |
142 // |
137 // |
143 CCalenExtraRowFormatter::CCalenExtraRowFormatter() |
138 CCalenExtraRowFormatter::CCalenExtraRowFormatter() |
144 { |
139 { |
145 OstTraceFunctionEntry0( DUP1_CCALENEXTRAROWFORMATTER_CCALENEXTRAROWFORMATTER_ENTRY ); |
140 TRACE_ENTRY_POINT; |
146 OstTraceFunctionExit0( DUP1_CCALENEXTRAROWFORMATTER_CCALENEXTRAROWFORMATTER_EXIT ); |
141 TRACE_EXIT_POINT; |
147 } |
142 } |
148 |
143 |
149 // ----------------------------------------------------------------------------- |
144 // ----------------------------------------------------------------------------- |
150 // CCalenExtraRowFormatter::ConstructL |
145 // CCalenExtraRowFormatter::ConstructL |
151 // ----------------------------------------------------------------------------- |
146 // ----------------------------------------------------------------------------- |
152 // |
147 // |
153 void CCalenExtraRowFormatter::ConstructL() |
148 void CCalenExtraRowFormatter::ConstructL() |
154 { |
149 { |
155 OstTraceFunctionEntry0( CCALENEXTRAROWFORMATTER_CONSTRUCTL_ENTRY ); |
150 TRACE_ENTRY_POINT; |
156 OstTraceFunctionExit0( CCALENEXTRAROWFORMATTER_CONSTRUCTL_EXIT ); |
151 TRACE_EXIT_POINT; |
157 } |
152 } |
158 |
153 |
159 |
154 |
160 // ----------------------------------------------------------------------------- |
155 // ----------------------------------------------------------------------------- |
161 // CCalenExtraRowFormatter::FormatExtraRowInformationL |
156 // CCalenExtraRowFormatter::FormatExtraRowInformationL |
162 // ----------------------------------------------------------------------------- |
157 // ----------------------------------------------------------------------------- |
163 // |
158 // |
164 EXPORT_C TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL( |
159 EXPORT_C TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL( |
165 CCalenLunarLocalizedInfo& aLocInfo, |
160 CCalenLunarLocalizedInfo& aLocInfo, |
166 RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields ) |
161 RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields, |
167 { |
162 TInt aMaxWidth, |
168 OstTraceFunctionEntry0( CCALENEXTRAROWFORMATTER_FORMATEXTRAROWINFORMATIONL_ENTRY ); |
163 const CFont& aFont |
169 if ( aPrioritizedFields.Count() == KZero) |
164 ,TBool aTwoLines |
170 { |
165 ) |
171 iText = KNullDesC; |
166 { |
172 |
167 TRACE_ENTRY_POINT; |
173 OstTraceFunctionExit0( CCALENEXTRAROWFORMATTER_FORMATEXTRAROWINFORMATIONL_EXIT ); |
168 |
174 return iText; |
169 |
175 } |
170 if ( aPrioritizedFields.Count() == 0) |
176 |
171 { |
177 // Initialize substring labels |
172 iText = KNullDesC; |
178 RArray<CCalenLunarLocalizedInfo::TField> subLabels; |
173 |
179 CleanupClosePushL( subLabels ); |
174 TRACE_EXIT_POINT; |
180 subLabels.AppendL( CCalenLunarLocalizedInfo::EAnimalYear ); |
175 return iText; |
181 subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarYear ); |
176 } |
182 subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay ); |
177 |
183 subLabels.AppendL( CCalenLunarLocalizedInfo::EFestival ); |
178 // Initialize substring labels |
184 subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm ); |
179 RArray<CCalenLunarLocalizedInfo::TField> subLabels; |
185 |
180 CleanupClosePushL( subLabels ); |
186 // ASSERT that all prioritized fields can be found from subLabels |
181 subLabels.AppendL( CCalenLunarLocalizedInfo::EAnimalYear ); |
187 for ( TInt i=0; i < aPrioritizedFields.Count(); i++) |
182 subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarYear ); |
188 { |
183 subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay ); |
189 ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= KZero ); |
184 subLabels.AppendL( CCalenLunarLocalizedInfo::EFestival ); |
190 } |
185 subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm ); |
191 |
186 |
192 // Initialize substring array |
187 // ASSERT that all prioritized fields can be found from subLabels |
193 CPtrCArray* subs = new (ELeave) CPtrCArray(10); |
188 for ( TInt i=0; i < aPrioritizedFields.Count(); i++) |
194 CleanupStack::PushL( subs ); |
189 { |
195 for ( TInt i = 0; i < subLabels.Count(); i++) |
190 ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= 0 ); |
196 { |
191 } |
197 subs->AppendL( TPtrC( KNullDesC ) ); |
192 |
198 } |
193 TBool fits = EFalse; |
199 |
194 |
200 // Set wanted fields to substring array |
195 do |
201 for ( TInt i = 0; i < aPrioritizedFields.Count(); i++) |
196 { |
202 { |
197 // Initialize substring array |
203 CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i]; |
198 CPtrCArray* subs = new (ELeave) CPtrCArray(10); |
204 TInt subIx = subLabels.Find( field ); |
199 CleanupStack::PushL( subs ); |
205 // Replace |
200 for ( TInt i = 0; i < subLabels.Count(); i++) |
206 subs->Delete(subIx); |
201 { |
207 RDebug::Print( _L("A sub count %d"), subs->Count() ); |
202 subs->AppendL( TPtrC( KNullDesC ) ); |
208 subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) ); |
203 } |
209 RDebug::Print( _L("B sub count %d"), subs->Count() ); |
204 // subs->InsertL( 0, TPtrC( KNullDesC ), 5 ); |
210 RDebug::Print( _L("B field %S"), &(subs->At(subIx)) ); |
205 |
211 |
206 // Set wanted fields to substring array |
212 } |
207 for ( TInt i = 0; i < aPrioritizedFields.Count(); i++) |
213 TBuf<100> textBuf; |
208 { |
214 QStringList textDataStringList; |
209 CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i]; |
215 for (TInt i=0; i < subLabels.Count(); i++) { |
210 TInt subIx = subLabels.Find( field ); |
216 textBuf = subs->At( i ); |
211 // Replace |
217 textDataStringList.append( |
212 subs->Delete(subIx); |
218 QString((QChar*)textBuf.Ptr(),textBuf.Length())); |
213 #ifdef _DEBUG |
219 } |
214 RDebug::Print( _L("A sub count %d"), subs->Count() ); |
220 // Get the locale specific separator |
215 #endif |
221 QString separator = hbTrId("txt_calendar_preview_title_cale_separator"); |
216 subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) ); |
222 |
217 #ifdef _DEBUG |
223 // Format all fields to single row |
218 RDebug::Print( _L("B sub count %d"), subs->Count() ); |
224 QString textDataString; |
219 RDebug::Print( _L("B field %S"), &(subs->At(subIx)) ); |
225 textDataString = hbTrId( |
220 #endif |
226 "txt_calendar_preview_title_123242526").arg( |
221 |
227 textDataStringList.at(0)).arg( |
222 } |
228 separator).arg( |
223 |
229 textDataStringList.at(1)).arg( |
224 // Format all fields to extra row |
230 textDataStringList.at(2)).arg( |
225 HBufC* extraRowFmt = StringLoader::LoadLC( R_CALE_EXTRA_ROW_LUNAR ); |
231 textDataStringList.at(3)).arg( |
226 |
232 textDataStringList.at(4)); |
227 #ifdef _DEBUG |
233 |
228 RDebug::RawPrint( *extraRowFmt ); |
234 iText = static_cast<const TUint16*> ( |
229 #endif |
235 textDataString.utf16()), textDataString.length(); |
230 |
236 |
231 TBuf<1000> fmt = *extraRowFmt; |
237 // Now we have something like "Year of Dog%2GengYin%2%2" |
232 for (TInt i=0; i < subLabels.Count(); i++) |
238 // where %2 is the separator txt_calendar_preview_title_cale_separator |
233 { |
239 // First We need to remove multiple occurences of separator |
234 #ifdef _DEBUG |
240 textBuf = static_cast<const TUint16*> ( |
235 RDebug::Print( _L("Before Format") ); |
241 separator.utf16()), separator.length(); |
236 RDebug::RawPrint( fmt ); |
242 CollapseDuplicatesL( iText, 0, textBuf); |
237 #endif |
243 |
238 StringLoader::Format( iText, |
244 // Remove leading and trailing separators |
239 fmt, |
245 // Leading separator won't be there but trailing one is there for sure |
240 i + 1, // %0U is a separator |
246 RemoveLeadingAndTrailingL( iText, textBuf ); |
241 subs->At( i ) ); |
247 |
242 fmt = iText; |
248 // CleanUp |
243 #ifdef _DEBUG |
249 CleanupStack::PopAndDestroy( subs ); |
244 RDebug::Print( _L("After Format") ); |
250 CleanupStack::PopAndDestroy( &subLabels ); |
245 RDebug::RawPrint( fmt ); |
251 |
246 #endif |
252 OstTraceFunctionExit0( DUP1_CCALENEXTRAROWFORMATTER_FORMATEXTRAROWINFORMATIONL_EXIT ); |
247 } |
253 return iText; |
248 |
254 } |
249 // Now we have something like "Year of Dog%0U%0U6/11%0U%0U" |
|
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 } |
255 |
362 |
256 //EOF |
363 //EOF |
257 |
364 |