|
1 /* |
|
2 * Copyright (c) 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 |
|
21 #include "uiarraysgenerator.h" |
|
22 |
|
23 #include "vcmodel.h" |
|
24 #include <badesca.h> |
|
25 #include <gulicon.h> |
|
26 #include <AknIconArray.h> |
|
27 #include <e32debug.h> |
|
28 #include <mmfcontrollerpluginresolver.h> // for CleanupResetAndDestroyPushL |
|
29 |
|
30 |
|
31 /** |
|
32 * @param aIcon. Ownership is tranfered. I.e. the icon will be destroyed, |
|
33 * when this object is destroyed, unless icon has been already removed by ReleaseIcon |
|
34 * @param aFirstLine |
|
35 * @param aSecondLine |
|
36 */ |
|
37 CUiEntryData* CUiEntryData::NewL( CGulIcon* aIcon, const TDesC& aFirstLine, |
|
38 const TDesC& aSecondLine ) |
|
39 { |
|
40 CUiEntryData* self = NewLC( aIcon, aFirstLine, aSecondLine ); |
|
41 CleanupStack::Pop( self ); |
|
42 return self; |
|
43 } |
|
44 |
|
45 CUiEntryData* CUiEntryData::NewLC( CGulIcon* aIcon, const TDesC& aFirstLine, |
|
46 const TDesC& aSecondLine ) |
|
47 { |
|
48 CUiEntryData* self = new (ELeave) CUiEntryData( aIcon, aFirstLine, aSecondLine ); |
|
49 CleanupStack::PushL( self ); |
|
50 return self; |
|
51 } |
|
52 |
|
53 CUiEntryData::CUiEntryData( CGulIcon* aIcon, const TDesC& aFirstLine, |
|
54 const TDesC& aSecondLine ) : |
|
55 iIcon( aIcon), iFirstLine( aFirstLine ), iSecondLine( aSecondLine ) |
|
56 { |
|
57 } |
|
58 |
|
59 CUiEntryData::~CUiEntryData() |
|
60 { |
|
61 delete iIcon; |
|
62 } |
|
63 |
|
64 CGulIcon* CUiEntryData::ReleaseIcon() |
|
65 { |
|
66 CGulIcon* result = iIcon; |
|
67 iIcon = NULL; |
|
68 return result; |
|
69 } |
|
70 |
|
71 const TDesC& CUiEntryData::FirstLine() const |
|
72 { |
|
73 return iFirstLine; |
|
74 } |
|
75 |
|
76 const TDesC& CUiEntryData::SecondLine() const |
|
77 { |
|
78 return iSecondLine; |
|
79 } |
|
80 |
|
81 TInt CUiEntryData::CompareByFirstLine( const CUiEntryData& aAnotherEntry ) const |
|
82 { |
|
83 return iFirstLine.CompareC( aAnotherEntry.FirstLine() ); |
|
84 } |
|
85 |
|
86 TInt CUiEntryData::CompareByFirstLine( const CUiEntryData& entry1, const CUiEntryData& entry2 ) |
|
87 { |
|
88 return entry1.CompareByFirstLine( entry2 ); |
|
89 } |
|
90 |
|
91 TDesC* CUiEntryData::ConstructListboxItemStringLC( TInt aIconIndex ) const |
|
92 { |
|
93 _LIT( KListboxStringPattern, "%d\t%S\t%S"); |
|
94 const TInt KPatternOverheadLength = 2; // two characters for tabs |
|
95 const TInt KMaxIconIndexLength = 5; // 5 characters for the decimal number represenation |
|
96 HBufC* result = HBufC::NewLC( KPatternOverheadLength + KMaxIconIndexLength + |
|
97 FirstLine().Length() + SecondLine().Length() ); |
|
98 result->Des().Format( KListboxStringPattern, aIconIndex, &FirstLine(), &SecondLine() ); |
|
99 return result; |
|
100 } |
|
101 |
|
102 CUiArraysGenerator* CUiArraysGenerator::NewLC() |
|
103 { |
|
104 CUiArraysGenerator* self = new (ELeave) CUiArraysGenerator; |
|
105 CleanupStack::PushL( self ); |
|
106 return self; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Create entries for all the subfolders in the given folder and sort the entries |
|
111 * alphabetically |
|
112 * @param aSource Model to parse |
|
113 * @param aCurrentFolderTitle KNullDesC for the main folder |
|
114 * @return The array of entries pushed to CS two times. First PopAndDestroy |
|
115 * will ResetAndDestroy, the second one will delete the array itself |
|
116 */ |
|
117 RPointerArray<CUiEntryData>* CUiArraysGenerator::ExtractFoldersAndSortLC2( const CVCModel& aSource, |
|
118 const TDesC& aCurrentFolderTitle ) |
|
119 { |
|
120 RPointerArray<CUiEntryData>* folderEntries = new ( ELeave ) RPointerArray<CUiEntryData>; |
|
121 CleanupStack::PushL( folderEntries ); |
|
122 CleanupResetAndDestroyPushL( *folderEntries ); |
|
123 if( aCurrentFolderTitle != KNullDesC() ) |
|
124 { |
|
125 return folderEntries; // no multilevel folders |
|
126 } |
|
127 |
|
128 for( TInt i = 0; i < aSource.Count(); i++ ) |
|
129 { |
|
130 if ( aSource.At(i).FolderTitle() != KNullDesC() ) |
|
131 { |
|
132 CUiEntryData* entry = CUiEntryData::NewL( aSource.At(i).FolderIconLC(), |
|
133 aSource.At(i).FolderListedName(), KNullDesC() ); |
|
134 CleanupStack::Pop(); // icon |
|
135 CleanupStack::PushL( entry ); |
|
136 TLinearOrder<CUiEntryData> order( CUiEntryData::CompareByFirstLine ); |
|
137 // does not allow duplicates |
|
138 TInt err = folderEntries->InsertInOrder( entry, order ); |
|
139 if( err != KErrNone ) |
|
140 { |
|
141 CleanupStack::PopAndDestroy( entry ); |
|
142 } |
|
143 else |
|
144 { |
|
145 CleanupStack::Pop( entry ); |
|
146 } |
|
147 } |
|
148 } |
|
149 return folderEntries; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Create entries for all the subfolders in the given folder and sort the entries |
|
154 * alphabetically |
|
155 * @param aSource Model to parse |
|
156 * @param aCurrentFolderTitle KNullDesC for the main folder |
|
157 * @return The array of entries pushed to CS two times. First PopAndDestroy |
|
158 * will ResetAndDestroy, the second one will delete the array itself |
|
159 */ |
|
160 RPointerArray<CUiEntryData>* CUiArraysGenerator::ExtractCommandsAndSortLC2( |
|
161 const CVCModel& aSource, const TDesC& aCurrentFolderTitle ) |
|
162 { |
|
163 RPointerArray<CUiEntryData>* commandEntries = new ( ELeave ) RPointerArray<CUiEntryData>; |
|
164 CleanupStack::PushL( commandEntries ); |
|
165 CleanupResetAndDestroyPushL( *commandEntries ); |
|
166 |
|
167 for( TInt i = 0; i < aSource.Count(); i++ ) |
|
168 { |
|
169 if ( aSource.At(i).FolderTitle() == aCurrentFolderTitle ) |
|
170 { |
|
171 CUiEntryData* entry = CUiEntryData::NewL( aSource.At(i).IconLC(), |
|
172 aSource.At(i).WrittenText(), aSource.At(i).AlternativeSpokenText() ); |
|
173 CleanupStack::Pop(); // icon |
|
174 CleanupStack::PushL( entry ); |
|
175 TLinearOrder<CUiEntryData> order( CUiEntryData::CompareByFirstLine ); |
|
176 commandEntries->InsertInOrderAllowRepeatsL( entry, order ); |
|
177 CleanupStack::Pop( entry ); |
|
178 } |
|
179 } |
|
180 return commandEntries; |
|
181 } |
|
182 |
|
183 /** |
|
184 * Parses the model into a set of arrays required by the VCommand app ui |
|
185 * I.e. basing on the "current folder" prepares the arrays for the listbox |
|
186 * If "current" folder is the main one, it means including all the sub-folders and main |
|
187 * folder commands. Otherwise, it means including all the current folder commands only |
|
188 * |
|
189 * Both folders and commands are alphabetially sorted. |
|
190 * All the target arrays are ResetAndDestroyed before the parsing |
|
191 * |
|
192 * @param aSource Model to parse |
|
193 * @param aCurrentFolderTitle KNullDesC if "curent" folder is the main one |
|
194 * @param aIconArray icons to display in the listbox. Order is not specified |
|
195 * @param aFolderTitles List of folder titles in the alphabetical order |
|
196 * @param aItemIsFolder List of boolean values for all the listbox elements from top |
|
197 * to bottom. ETrue for every folder, EFalse for every command |
|
198 * @param aItemArray List of strings in the listbox expected |
|
199 * format ( @see LB_SINGLE_GRAPHIC_HEADING ). Includes: |
|
200 * * a refence to the icon - zero-based index from the aIconArray |
|
201 * * written text of the command |
|
202 * * user-specified alternative text or KNullDesC if |
|
203 * |
|
204 */ |
|
205 void CUiArraysGenerator::FillArraysL( const CVCModel& aSource, const TDesC& aCurrentFolderTitle, |
|
206 CAknIconArray& aIconArray, CDesC16ArrayFlat& aFolderTitles, |
|
207 RArray<TBool>& aItemIsFolder, CDesC16ArrayFlat& aItemArray ) |
|
208 { |
|
209 CleanupClosePushL( aItemIsFolder ); |
|
210 |
|
211 aIconArray.ResetAndDestroy(); |
|
212 aFolderTitles.Reset(); |
|
213 aItemIsFolder.Reset(); |
|
214 aItemArray.Reset(); |
|
215 // Get folder entries |
|
216 |
|
217 RPointerArray<CUiEntryData>* entries = |
|
218 ExtractFoldersAndSortLC2( aSource, aCurrentFolderTitle ); |
|
219 |
|
220 FillFolderTitlesL( aItemIsFolder, aFolderTitles, aSource, *entries ); |
|
221 |
|
222 // get command entries |
|
223 RPointerArray<CUiEntryData>* ordinaryCommands = |
|
224 ExtractCommandsAndSortLC2( aSource, aCurrentFolderTitle ); |
|
225 |
|
226 // append commands to folder list |
|
227 while( ordinaryCommands->Count() > 0 ) |
|
228 { |
|
229 entries->AppendL( (*ordinaryCommands)[0] ); |
|
230 aItemIsFolder.AppendL( EFalse ); |
|
231 ordinaryCommands->Remove( 0 ); |
|
232 } |
|
233 |
|
234 CleanupStack::PopAndDestroy( ordinaryCommands ); // ResetAndDestroy |
|
235 CleanupStack::PopAndDestroy( ordinaryCommands ); // delete |
|
236 |
|
237 for( TInt i = 0; i < entries->Count(); i++ ) |
|
238 { |
|
239 aIconArray.AppendL( (*entries)[i]->ReleaseIcon() ); |
|
240 TDesC* string = (*entries)[i]->ConstructListboxItemStringLC( i ); |
|
241 aItemArray.AppendL( *string ); |
|
242 CleanupStack::PopAndDestroy( string ); |
|
243 } |
|
244 |
|
245 CleanupStack::PopAndDestroy( entries ); // ResetAndDestroy |
|
246 CleanupStack::PopAndDestroy( entries ); // delete |
|
247 |
|
248 CleanupStack::Pop(); //aItemIsFolder |
|
249 } |
|
250 |
|
251 /** |
|
252 * Updates a set of arrays required by the VCommand app ui to match current folder names |
|
253 * I.e. basing on the "current folder" prepares the arrays for the listbox |
|
254 * If "current" folder is the main one, it means including all the sub-folders and main |
|
255 * folder commands. Otherwise, it means including all the current folder commands only |
|
256 * |
|
257 * Both folders and commands are alphabetially sorted. |
|
258 * |
|
259 * @param aSource Model to parse |
|
260 * @param aCurrentFolderTitle KNullDesC if "curent" folder is the main one |
|
261 * @param aIconArray icons to display in the listbox. Order is not specified |
|
262 * @param aFolderTitles List of folder titles in the alphabetical order |
|
263 * @param aItemIsFolder List of boolean values for all the listbox elements from top |
|
264 * to bottom. ETrue for every folder, EFalse for every command |
|
265 * @param aItemArray List of strings in the listbox expected |
|
266 * format ( @see LB_SINGLE_GRAPHIC_HEADING ). Includes: |
|
267 * * a refence to the icon - zero-based index from the aIconArray |
|
268 * * written text of the command |
|
269 * * user-specified alternative text or KNullDesC if |
|
270 * |
|
271 */ |
|
272 void CUiArraysGenerator::UpdateFolderArraysL( const CVCModel& aSource, const TDesC& aCurrentFolderTitle, |
|
273 CAknIconArray& aIconArray, CDesC16ArrayFlat& aFolderTitles, |
|
274 RArray<TBool>& aItemIsFolder, CDesC16ArrayFlat& aItemArray ) |
|
275 { |
|
276 // Get folder entries |
|
277 RPointerArray<CUiEntryData>* entries = |
|
278 ExtractFoldersAndSortLC2( aSource, aCurrentFolderTitle ); |
|
279 |
|
280 for( TInt i = aItemIsFolder.Count() - 1; i >= 0 ; i-- ) |
|
281 { |
|
282 if ( aItemIsFolder[i] ) |
|
283 { |
|
284 aFolderTitles.Delete(i); |
|
285 aItemArray.Delete(i); |
|
286 aItemIsFolder.Remove(i); |
|
287 } |
|
288 } |
|
289 |
|
290 for( TInt i = 0; i < entries->Count(); i++ ) |
|
291 { |
|
292 aItemIsFolder.InsertL( ETrue, i ); |
|
293 |
|
294 TBool titleForCurrentListedNameFound = EFalse; |
|
295 for( TInt j = 0; j < aSource.Count(); j++ ) |
|
296 { |
|
297 if( aSource.At(j).FolderListedName() == (*entries)[i]->FirstLine() ) |
|
298 { |
|
299 aFolderTitles.InsertL( i, aSource.At(j).FolderTitle() ); |
|
300 titleForCurrentListedNameFound = ETrue; |
|
301 break; |
|
302 } |
|
303 } |
|
304 if (!titleForCurrentListedNameFound) |
|
305 { |
|
306 User::Leave( KErrCorrupt ); |
|
307 } |
|
308 |
|
309 aIconArray.AppendL( (*entries)[i]->ReleaseIcon() ); |
|
310 |
|
311 TDesC* string = (*entries)[i]->ConstructListboxItemStringLC( aIconArray.Count() - 1 ); |
|
312 aItemArray.InsertL( i, *string ); |
|
313 CleanupStack::PopAndDestroy( string ); |
|
314 } |
|
315 |
|
316 CleanupStack::PopAndDestroy( entries ); // ResetAndDestroy |
|
317 CleanupStack::PopAndDestroy( entries ); // delete |
|
318 } |
|
319 |
|
320 /** |
|
321 * |
|
322 */ |
|
323 void CUiArraysGenerator::FillFolderTitlesL( RArray<TBool>& aItemIsFolder, |
|
324 CDesC16ArrayFlat& aFolderTitles, const CVCModel& aModel, |
|
325 const RPointerArray<CUiEntryData>& aEntries ) |
|
326 { |
|
327 // fill folder titles |
|
328 for( TInt i = 0; i < aEntries.Count(); i++ ) |
|
329 { |
|
330 aItemIsFolder.AppendL( ETrue ); |
|
331 TBool titleForCurrentListedNameFound = EFalse; |
|
332 for( TInt j = 0; j < aModel.Count(); j++ ) |
|
333 { |
|
334 if( aModel.At(j).FolderListedName() == aEntries[i]->FirstLine() ) |
|
335 { |
|
336 aFolderTitles.AppendL( aModel.At(j).FolderTitle() ); |
|
337 titleForCurrentListedNameFound = ETrue; |
|
338 break; |
|
339 } // if |
|
340 } // for j |
|
341 if (!titleForCurrentListedNameFound) |
|
342 { |
|
343 User::Leave( KErrCorrupt ); |
|
344 } |
|
345 } // for i |
|
346 } |
|
347 |
|
348 // End of File |