|
1 /* |
|
2 * Copyright (c) 2005 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: Client's Results command buffering |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "NSmlResultsContainer.h" |
|
21 #include "NSmlCmdsBase.h" |
|
22 #include "nsmlcliagconstants.h" |
|
23 |
|
24 // --------------------------------------------------------- |
|
25 // CNSmlResultsContainer::CNSmlResultsContainer |
|
26 // Constructor, nothing special in here. |
|
27 // --------------------------------------------------------- |
|
28 // |
|
29 CNSmlResultsContainer::CNSmlResultsContainer() |
|
30 { |
|
31 } |
|
32 |
|
33 // --------------------------------------------------------- |
|
34 // CNSmlResultsContainer::~CNSmlResultsContainer() |
|
35 // Destructor |
|
36 // --------------------------------------------------------- |
|
37 // |
|
38 CNSmlResultsContainer::~CNSmlResultsContainer() |
|
39 { |
|
40 if ( !iResultsArray ) |
|
41 { |
|
42 return; |
|
43 } |
|
44 TInt arrayCount = iResultsArray->Count(); |
|
45 for ( TInt i = 0; i < arrayCount ; i++ ) |
|
46 { |
|
47 FreeResources( i ); |
|
48 } |
|
49 delete iResultsArray; |
|
50 } |
|
51 |
|
52 // --------------------------------------------------------- |
|
53 // CNSmlResultsContainer::NewL() |
|
54 // Creates new instance of CNSmlResultsContainer. |
|
55 // Does not leave instance pointer to CleanupStack. |
|
56 // --------------------------------------------------------- |
|
57 // |
|
58 CNSmlResultsContainer* CNSmlResultsContainer::NewL( CNSmlCmdsBase* aSyncMLCmds) |
|
59 { |
|
60 CNSmlResultsContainer* self = new (ELeave) CNSmlResultsContainer(); |
|
61 CleanupStack::PushL( self ); |
|
62 self->ConstructL( aSyncMLCmds ); |
|
63 CleanupStack::Pop(); //self |
|
64 return( self ); |
|
65 } |
|
66 |
|
67 // --------------------------------------------------------- |
|
68 // CNSmlResultsContainer::CreateNewResultsL() |
|
69 // Creates new empty Results element if needed. |
|
70 // --------------------------------------------------------- |
|
71 // |
|
72 EXPORT_C TInt CNSmlResultsContainer::CreateNewResultsL( const TDesC8& aMsgRef, const TDesC8& aCmdRef, const SmlTarget_t* aTargetRef, const SmlSource_t* aSourceRef ) |
|
73 { |
|
74 TResultsData resultsData; |
|
75 resultsData.iMsgRef = NULL; |
|
76 resultsData.iCmdRef = NULL; |
|
77 resultsData.iResults = NULL; |
|
78 resultsData.iWasWritten = EFalse; |
|
79 resultsData.iStatusRef = -1; |
|
80 iResultsArray->AppendL( resultsData ); |
|
81 TInt i = iResultsArray->Count() - 1; |
|
82 (*iResultsArray)[i].iMsgRef = aMsgRef.AllocL(); |
|
83 (*iResultsArray)[i].iCmdRef = aCmdRef.AllocL(); |
|
84 (*iResultsArray)[i].iResults = new( ELeave ) SmlResults_t; |
|
85 (*iResultsArray)[i].iResults->elementType = SML_PE_RESULTS; |
|
86 iSyncMLCmds->PcdataNewL( (*iResultsArray)[i].iResults->msgRef, aMsgRef ); |
|
87 iSyncMLCmds->PcdataNewL( (*iResultsArray)[i].iResults->cmdRef, aCmdRef ); |
|
88 if ( aTargetRef ) |
|
89 { |
|
90 if ( aTargetRef->locURI ) |
|
91 { |
|
92 CopyPcdataL( aTargetRef->locURI, (*iResultsArray)[i].iResults->targetRef ); |
|
93 } |
|
94 } |
|
95 if ( aSourceRef ) |
|
96 { |
|
97 if ( aSourceRef->locURI ) |
|
98 { |
|
99 CopyPcdataL( aSourceRef->locURI, (*iResultsArray)[i].iResults->sourceRef ); |
|
100 } |
|
101 } |
|
102 return iResultsArray->Count(); |
|
103 } |
|
104 // --------------------------------------------------------- |
|
105 // CNSmlResultsContainer::SetCmdIDL() |
|
106 // Sets CmdID element |
|
107 // --------------------------------------------------------- |
|
108 // |
|
109 EXPORT_C void CNSmlResultsContainer::SetCmdIDL( TInt aEntryID, const SmlPcdata_t* aCmdID ) |
|
110 { |
|
111 delete (*iResultsArray)[aEntryID-1].iResults->cmdID; |
|
112 (*iResultsArray)[aEntryID-1].iResults->cmdID = NULL; |
|
113 CopyPcdataL( aCmdID, (*iResultsArray)[aEntryID-1].iResults->cmdID ); |
|
114 } |
|
115 |
|
116 // --------------------------------------------------------- |
|
117 // CNSmlResultsContainer::AddItemL() |
|
118 // Adds Item/Data element |
|
119 // --------------------------------------------------------- |
|
120 EXPORT_C void CNSmlResultsContainer::AddItemL( TInt aEntryID, const CBufBase& aObject, const TDesC8& aType, const TDesC8& aFormat ) |
|
121 { |
|
122 (*iResultsArray)[aEntryID-1].iResults->itemList = new( ELeave ) SmlItemList_t; |
|
123 (*iResultsArray)[aEntryID-1].iResults->itemList->item = new( ELeave ) SmlItem_t; |
|
124 iSyncMLCmds->PcdataNewL( (*iResultsArray)[aEntryID-1].iResults->itemList->item->data, CONST_CAST( CBufBase&, aObject ).Ptr( 0 ) ); |
|
125 if ( (*iResultsArray)[aEntryID-1].iResults->targetRef ) |
|
126 { |
|
127 HBufC* source; |
|
128 iSyncMLCmds->PCDATAToUnicodeLC( *(*iResultsArray)[aEntryID-1].iResults->targetRef, source ); |
|
129 iSyncMLCmds->DoSourceL( (*iResultsArray)[aEntryID-1].iResults->itemList->item->source, *source ); |
|
130 CleanupStack::PopAndDestroy(); // source |
|
131 } |
|
132 if ( aType.Length() > 0 || aFormat.Length() > 0 ) |
|
133 { |
|
134 SmlMetInfMetInf_t* metInf; |
|
135 iSyncMLCmds->DoMetInfLC( metInf ); |
|
136 if ( aType.Length() > 0 ) |
|
137 { |
|
138 iSyncMLCmds->PcdataNewL( metInf->type, aType ); |
|
139 } |
|
140 if ( aFormat.Length() > 0 ) |
|
141 { |
|
142 iSyncMLCmds->PcdataNewL( metInf->format, aFormat ); |
|
143 } |
|
144 iSyncMLCmds->DoMetaL( (*iResultsArray)[aEntryID-1].iResults->itemList->item->meta, metInf ); |
|
145 CleanupStack::Pop(); //metInf |
|
146 } |
|
147 } |
|
148 // --------------------------------------------------------- |
|
149 // CNSmlResultsContainer::SetStatusID() |
|
150 // Set Id of status buffer. The Id is an entry to CNSmlStatusContainer buffer |
|
151 // --------------------------------------------------------- |
|
152 EXPORT_C void CNSmlResultsContainer::SetStatusID( TInt aEntryID, TInt aStatusEntryID ) |
|
153 { |
|
154 (*iResultsArray)[aEntryID-1].iStatusRef = aStatusEntryID; |
|
155 } |
|
156 |
|
157 // --------------------------------------------------------- |
|
158 // CNSmlResultsContainer::RemoveWritten() |
|
159 // Marks Results command written in outgoing XML document |
|
160 // --------------------------------------------------------- |
|
161 EXPORT_C void CNSmlResultsContainer::RemoveWritten( TInt aEntryID ) |
|
162 { |
|
163 (*iResultsArray)[aEntryID-1].iWasWritten = ETrue; |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------- |
|
167 // CNSmlResultsContainer::Begin |
|
168 // Sets index for reading to starting value |
|
169 // --------------------------------------------------------- |
|
170 EXPORT_C void CNSmlResultsContainer::Begin() |
|
171 { |
|
172 iArrayInd = -1; |
|
173 } |
|
174 // --------------------------------------------------------- |
|
175 // CNSmlResultsContainer::NextResultsElement() |
|
176 // Returns pointer to the next Results element |
|
177 // --------------------------------------------------------- |
|
178 EXPORT_C TBool CNSmlResultsContainer::NextResultsElement( SmlResults_t*& aResults ) |
|
179 { |
|
180 TBool moreElements( ETrue ); |
|
181 while ( moreElements ) |
|
182 { |
|
183 iArrayInd++; |
|
184 if ( iArrayInd > iResultsArray->Count() - 1 ) |
|
185 { |
|
186 RemoveAllWrittenOnes(); |
|
187 iArrayInd = -1; |
|
188 moreElements = EFalse; |
|
189 } |
|
190 else |
|
191 { |
|
192 if ( ( !(*iResultsArray)[iArrayInd].iWasWritten ) && ( (*iResultsArray)[iArrayInd].iResults->itemList) ) |
|
193 { |
|
194 aResults = (*iResultsArray)[iArrayInd].iResults; |
|
195 return ETrue; |
|
196 } |
|
197 } |
|
198 } |
|
199 return moreElements; |
|
200 } |
|
201 // --------------------------------------------------------- |
|
202 // CNSmlResultsContainer::CurrentEntryID() |
|
203 // Returns current results ID |
|
204 // --------------------------------------------------------- |
|
205 EXPORT_C TInt CNSmlResultsContainer::CurrentEntryID() const |
|
206 { |
|
207 return( iArrayInd + 1); |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------- |
|
211 // CNSmlResultsContainer::AnyResults() |
|
212 // |
|
213 // --------------------------------------------------------- |
|
214 EXPORT_C TBool CNSmlResultsContainer::AnyResults() const |
|
215 { |
|
216 TInt arrayCount( iResultsArray->Count() ); |
|
217 |
|
218 for ( TInt i = 0; i < arrayCount ; i++ ) |
|
219 { |
|
220 if ( ( !(*iResultsArray)[i].iWasWritten ) && ( (*iResultsArray)[i].iResults->itemList) ) |
|
221 { |
|
222 return ETrue; |
|
223 } |
|
224 } |
|
225 |
|
226 return EFalse; |
|
227 } |
|
228 // --------------------------------------------------------- |
|
229 // CNSmlResultsContainer::StatusID() |
|
230 // |
|
231 // --------------------------------------------------------- |
|
232 EXPORT_C TInt CNSmlResultsContainer::StatusID( TInt aEntryID ) const |
|
233 { |
|
234 return (*iResultsArray)[aEntryID-1].iStatusRef; |
|
235 } |
|
236 // |
|
237 // private methods |
|
238 // |
|
239 |
|
240 // --------------------------------------------------------- |
|
241 // CNSmlResultsContainer::ConstructL() |
|
242 // Two-way construction. Constructor may leave in EPOC. |
|
243 // --------------------------------------------------------- |
|
244 // |
|
245 void CNSmlResultsContainer::ConstructL( CNSmlCmdsBase* aSyncMLCmds ) |
|
246 { |
|
247 iSyncMLCmds = aSyncMLCmds; |
|
248 iResultsArray = new( ELeave ) CArrayFixFlat<TResultsData>(KNSmlResultsArrayGranularity); |
|
249 iArrayInd = -1; |
|
250 } |
|
251 // --------------------------------------------------------- |
|
252 // CNSmlResultsContainer::CopyPcdataL() |
|
253 // Deep copy to Pcdata element |
|
254 // Copied element is not pushed to Cleanup stack |
|
255 // --------------------------------------------------------- |
|
256 // |
|
257 void CNSmlResultsContainer::CopyPcdataL( const SmlPcdata_t* aFromPcdata, SmlPcdata_t*& aToPcdata ) const |
|
258 { |
|
259 aToPcdata = NULL; |
|
260 if ( aFromPcdata ) |
|
261 { |
|
262 SmlPcdata_t* newPcdata = new( ELeave ) SmlPcdata_t; |
|
263 CleanupStack::PushL( newPcdata ); |
|
264 newPcdata->length = aFromPcdata->length; |
|
265 newPcdata->contentType = SML_PCDATA_OPAQUE; |
|
266 newPcdata->extension = SML_EXT_UNDEFINED; |
|
267 if ( aFromPcdata->content ) |
|
268 { |
|
269 newPcdata->content = User::AllocL( newPcdata->length ); |
|
270 TPtr8 fromPtr ( (TUint8*) aFromPcdata->content, aFromPcdata->length ); |
|
271 fromPtr.SetLength( fromPtr.MaxLength() ); |
|
272 TPtr8 toPtr ( (TUint8*) newPcdata->content, newPcdata->length ); |
|
273 toPtr.SetLength( toPtr.MaxLength() ); |
|
274 toPtr = fromPtr; |
|
275 } |
|
276 CleanupStack::Pop(); //newPcdata |
|
277 aToPcdata = newPcdata; |
|
278 } |
|
279 } |
|
280 |
|
281 // --------------------------------------------------------- |
|
282 // CNSmlResultsContainer::RemoveAllWrittenOnes() |
|
283 // Removes all written Results elements |
|
284 // --------------------------------------------------------- |
|
285 void CNSmlResultsContainer::RemoveAllWrittenOnes() |
|
286 { |
|
287 for ( TInt i = iResultsArray->Count() - 1; i >= 0; i--) |
|
288 { |
|
289 if ( (*iResultsArray)[i].iWasWritten ) |
|
290 { |
|
291 FreeResources( i ); |
|
292 iResultsArray->Delete( i ); |
|
293 } |
|
294 } |
|
295 iResultsArray->Compress(); |
|
296 iArrayInd = -1; |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------- |
|
300 // CNSmlResultsContainer::FreeResources() |
|
301 // Frees resources of an item |
|
302 // --------------------------------------------------------- |
|
303 void CNSmlResultsContainer::FreeResources( TInt aI ) |
|
304 { |
|
305 delete (*iResultsArray)[aI].iMsgRef; |
|
306 delete (*iResultsArray)[aI].iCmdRef; |
|
307 delete (*iResultsArray)[aI].iResults; |
|
308 } |