|
1 /* |
|
2 * Copyright (c) 2006-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 the License "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: Iterator in the output of GetList |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "contactiterator.h" |
|
20 #include "contactinterface.hrh" |
|
21 |
|
22 /* |
|
23 ----------------------------------------------------------------------------- |
|
24 CContactIterator::~CContactIterator() |
|
25 Description : Destructor |
|
26 Return values : N/A |
|
27 ----------------------------------------------------------------------------- |
|
28 */ |
|
29 CContactIterator::~CContactIterator() |
|
30 { |
|
31 delete iContactIter; |
|
32 } |
|
33 |
|
34 |
|
35 /* |
|
36 ----------------------------------------------------------------------------- |
|
37 CContactIterator::NewL() |
|
38 Description : Allocates memory |
|
39 Return values : Returns class instance |
|
40 ----------------------------------------------------------------------------- |
|
41 */ |
|
42 |
|
43 CContactIterator* CContactIterator::NewL(CContactIter* aIter) |
|
44 { |
|
45 return (new (ELeave) CContactIterator(aIter)); |
|
46 } |
|
47 |
|
48 |
|
49 /* |
|
50 ----------------------------------------------------------------------------- |
|
51 CContactIterator::CContactIterator() |
|
52 Description : Constructor- Initializes data |
|
53 Return values : N/A |
|
54 ----------------------------------------------------------------------------- |
|
55 */ |
|
56 CContactIterator::CContactIterator(CContactIter* aIter) |
|
57 { |
|
58 iContactIter = aIter; |
|
59 iIndex = -1; |
|
60 } |
|
61 |
|
62 /* |
|
63 ----------------------------------------------------------------------------- |
|
64 CContactIterator::Reset() |
|
65 Description : Resets the cursor of the iterator to -1 |
|
66 Return values : N/A |
|
67 ----------------------------------------------------------------------------- |
|
68 */ |
|
69 void CContactIterator:: Reset() |
|
70 { |
|
71 iIndex=-1; |
|
72 } |
|
73 |
|
74 /* |
|
75 ----------------------------------------------------------------------------- |
|
76 CContactIterator::GetContactIDUTF() |
|
77 Description : Converts to UTF format |
|
78 Return values : N/A |
|
79 ----------------------------------------------------------------------------- |
|
80 */ |
|
81 void CContactIterator :: ContactIDToUTF(HBufC* aContactID) |
|
82 { |
|
83 TInt len = aContactID->Length(); |
|
84 TUint16* ptr16 = const_cast<TUint16*> (aContactID->Des().Ptr()); |
|
85 for(TInt i=0; i<len; i++) |
|
86 { |
|
87 ptr16[i] = ptr16[i] | 0xE000; |
|
88 } |
|
89 } |
|
90 |
|
91 /* |
|
92 ----------------------------------------------------------------------------- |
|
93 CContactIterator::NextL() |
|
94 Description : returns in outparam, the next item in the list that iterator |
|
95 is iterating on |
|
96 Return values : ETrue/EFalse based on success or failure |
|
97 ----------------------------------------------------------------------------- |
|
98 */ |
|
99 TBool CContactIterator::NextL(TLiwVariant& aEntry) |
|
100 { |
|
101 |
|
102 CLiwMap* pContactMap = NULL; |
|
103 TBool retVal = EFalse; |
|
104 |
|
105 //If iterator is iterating over contacts |
|
106 if(iContactIter->iIndicator == EContacts) |
|
107 { |
|
108 CSingleContact* contact = NULL; |
|
109 HBufC8* cntId = NULL ; |
|
110 CSingleContactField* field = NULL; |
|
111 HBufC* cntIdUnicode = NULL; |
|
112 |
|
113 //Get the next contact on the list |
|
114 iContactIter->NextL(contact, cntId); |
|
115 |
|
116 if(contact) |
|
117 { |
|
118 TPtrC8 fieldName; |
|
119 TPtrC fieldLabel; |
|
120 TPtrC fieldValue; |
|
121 TTime fieldTime; |
|
122 TBool date = EFalse; |
|
123 |
|
124 CleanupStack :: PushL(contact); |
|
125 |
|
126 pContactMap = CLiwDefaultMap::NewL(); |
|
127 CleanupStack :: PushL(pContactMap); |
|
128 |
|
129 cntIdUnicode = HBufC::NewL(cntId->Length()); |
|
130 CleanupStack :: PushL(cntIdUnicode); |
|
131 cntIdUnicode->Des().Copy(*cntId); |
|
132 delete cntId; |
|
133 cntId = NULL; |
|
134 ContactIDToUTF(cntIdUnicode); |
|
135 pContactMap->InsertL(KContactId,TLiwVariant(*cntIdUnicode)); |
|
136 CleanupStack :: Pop(cntIdUnicode); |
|
137 |
|
138 TLiwVariant val; |
|
139 CleanupStack::PushL( TCleanupItem( TLiwVariant::VariantCleanup , &val) ); |
|
140 |
|
141 TInt fieldCount = contact->FieldCount(); |
|
142 for(TInt i=0 ; i< fieldCount ; i++) |
|
143 { |
|
144 CLiwMap* pFieldLinkedMap = CLiwDefaultMap::NewL(); |
|
145 CleanupClosePushL(*pFieldLinkedMap); |
|
146 //Get the next field |
|
147 field = contact->FieldAt(i); |
|
148 |
|
149 //Get field data and populate the map structure |
|
150 field->GetFieldDataL(fieldName, fieldLabel, fieldValue); |
|
151 if((fieldName.Compare(KDate) == 0) || (fieldName.Compare(KAnniversary) == 0)) |
|
152 { |
|
153 date = ETrue; |
|
154 fieldTime = field->GetDateTime(); |
|
155 } |
|
156 |
|
157 if(EFalse != pContactMap->FindL(fieldName,val)) |
|
158 { |
|
159 //In case there are multiple values for a given Fieldkey, add "Next" key to the map. |
|
160 // and create a linked list of label-value pairs for a given key |
|
161 pFieldLinkedMap->InsertL(KFieldLabel,TLiwVariant(fieldLabel)); |
|
162 if(date) |
|
163 { |
|
164 pFieldLinkedMap->InsertL(KFieldValue,TLiwVariant(fieldTime)); |
|
165 } |
|
166 else |
|
167 { |
|
168 pFieldLinkedMap->InsertL(KFieldValue,TLiwVariant(fieldValue)); |
|
169 } |
|
170 pFieldLinkedMap->InsertL(KFieldNext,TLiwVariant(val)); |
|
171 } |
|
172 else |
|
173 { |
|
174 //In case its the first time the FieldKey is being read, just have label-value pair inserted |
|
175 pFieldLinkedMap->InsertL(KFieldLabel,TLiwVariant(fieldLabel)); |
|
176 if(date) |
|
177 { |
|
178 date = EFalse; |
|
179 pFieldLinkedMap->InsertL(KFieldValue,TLiwVariant(fieldTime)); |
|
180 } |
|
181 else |
|
182 { |
|
183 pFieldLinkedMap->InsertL(KFieldValue,TLiwVariant(fieldValue)); |
|
184 } |
|
185 } |
|
186 //For the given Fieldkey insert the label-value pair |
|
187 pContactMap->InsertL(fieldName, TLiwVariant(pFieldLinkedMap)); |
|
188 CleanupStack::PopAndDestroy(pFieldLinkedMap); |
|
189 } |
|
190 CleanupStack::Pop(&val); |
|
191 val.Reset(); |
|
192 CleanupStack::Pop(pContactMap); |
|
193 CleanupStack::PopAndDestroy(contact); |
|
194 retVal = ETrue; |
|
195 } |
|
196 else |
|
197 { |
|
198 delete cntId; |
|
199 } |
|
200 } |
|
201 //In case the iterator is iterating over a list of groups |
|
202 else if(iContactIter->iIndicator == EGroups) |
|
203 { |
|
204 CSingleContact* contact; |
|
205 HBufC8* grpID; |
|
206 CSingleContactField* field; |
|
207 HBufC* grpUnicode = NULL; |
|
208 //Get the next group |
|
209 iContactIter->NextL(contact, grpID); |
|
210 |
|
211 if(contact) |
|
212 { |
|
213 CleanupStack::PushL(contact); |
|
214 |
|
215 TPtrC groupLabel; |
|
216 RPointerArray<TDesC8> array; |
|
217 CleanupClosePushL(array); |
|
218 pContactMap = CLiwDefaultMap::NewL(); |
|
219 CleanupStack::PushL(pContactMap); |
|
220 |
|
221 grpUnicode = HBufC::NewL(grpID->Length()); |
|
222 grpUnicode->Des().Copy(*grpID); |
|
223 delete grpID; |
|
224 grpID = NULL; |
|
225 ContactIDToUTF(grpUnicode); |
|
226 CleanupStack::PushL(grpUnicode); |
|
227 //Insert the contact id |
|
228 pContactMap->InsertL(KGroupId,TLiwVariant(*grpUnicode)); |
|
229 CleanupStack::Pop(grpUnicode); |
|
230 |
|
231 field = contact->FieldAt(0); |
|
232 |
|
233 //Get the field data of the group |
|
234 field->GetFieldDataL(groupLabel, array); |
|
235 |
|
236 //Insert group label |
|
237 pContactMap->InsertL(KGroupLabel,TLiwVariant(groupLabel)); |
|
238 |
|
239 CLiwList* pContentsList = CLiwDefaultList::NewL(); |
|
240 CleanupClosePushL(*pContentsList); |
|
241 TInt count = array.Count(); |
|
242 if(count) |
|
243 { |
|
244 for(TInt i=0; i<count ; i++) |
|
245 { |
|
246 HBufC* cntidUni = HBufC:: NewL(array[i]->Length()); |
|
247 cntidUni->Des().Copy(*array[i]); |
|
248 CleanupStack::PushL(cntidUni); |
|
249 ContactIDToUTF(cntidUni); |
|
250 pContentsList->AppendL(*cntidUni); |
|
251 CleanupStack::Pop(cntidUni); |
|
252 } |
|
253 //Insert the list of contacts for the given group |
|
254 pContactMap->InsertL(KContents,TLiwVariant(pContentsList)); |
|
255 } |
|
256 CleanupStack::PopAndDestroy(pContentsList); |
|
257 CleanupStack :: Pop(pContactMap); |
|
258 CleanupStack :: PopAndDestroy(); |
|
259 CleanupStack :: PopAndDestroy(contact); |
|
260 |
|
261 retVal = ETrue; |
|
262 } |
|
263 else |
|
264 { |
|
265 delete grpID; |
|
266 } |
|
267 } |
|
268 //In case iterator is iterating over list of databases |
|
269 else if(iContactIter->iIndicator == EDatabase) |
|
270 { |
|
271 TPtrC DbName; |
|
272 //Get the next database name |
|
273 iContactIter->NextL(DbName); |
|
274 if(DbName.Compare(KNullDesC) != 0) |
|
275 { |
|
276 pContactMap = CLiwDefaultMap::NewL(); |
|
277 CleanupStack::PushL(pContactMap); |
|
278 //Insert DbName into the map |
|
279 pContactMap->InsertL(KDBUri,TLiwVariant(DbName)); |
|
280 CleanupStack::Pop(pContactMap); |
|
281 retVal = ETrue; |
|
282 } |
|
283 } |
|
284 if(pContactMap) |
|
285 { |
|
286 CleanupClosePushL(*pContactMap); |
|
287 aEntry.SetL(pContactMap); |
|
288 CleanupStack::PopAndDestroy(pContactMap); |
|
289 } |
|
290 return retVal; |
|
291 } |