First public contribution.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <caf/caftypes.h>
23 #include <caf/cafmimeheader.h>
24 #include "mimefieldanddata.h"
26 using namespace ContentAccess;
28 EXPORT_C CCafMimeHeader* CCafMimeHeader::NewL(const TDesC8 &aContentType)
30 CCafMimeHeader *self = new (ELeave) CCafMimeHeader();
31 CleanupStack::PushL(self);
32 self->ConstructL(aContentType);
33 CleanupStack::Pop(self);
37 CCafMimeHeader::CCafMimeHeader()
41 void CCafMimeHeader::ConstructL(const TDesC8 &aContentType)
43 SetStandardMimeDataL(EContentType, aContentType);
46 EXPORT_C CCafMimeHeader::~CCafMimeHeader()
49 iNonStandardMimeHeaders.ResetAndDestroy();
50 iNonStandardMimeHeaders.Close();
52 for(i = 0; i < EMimeMax; i++)
54 delete iStandardMimeHeader[i];
58 EXPORT_C void CCafMimeHeader::SetStandardMimeDataL(const TMimeFields &aIndex, const TDesC8& aData)
60 HBufC8 *data = aData.AllocLC();
62 // make sure mime types are lower case
63 if(aIndex == EContentType)
65 data->Des().LowerCase();
68 // replace whatever value was there before
69 delete iStandardMimeHeader[aIndex];
70 iStandardMimeHeader[aIndex] = data;
72 CleanupStack::Pop(data);
75 EXPORT_C TPtrC8 CCafMimeHeader::StandardMimeData(const TMimeFields &aIndex) const
77 // If the field value has not been set, return a null string
78 if(iStandardMimeHeader[aIndex] == NULL)
82 return TPtrC8(iStandardMimeHeader[aIndex]->Des());
86 EXPORT_C TInt CCafMimeHeader::NonStandardMimeCount() const
88 return iNonStandardMimeHeaders.Count();
91 EXPORT_C void CCafMimeHeader::AddNonStandardMimeL(const TDesC8& aFieldName, const TDesC8& aData)
93 CMimeFieldAndData *mimeData = CMimeFieldAndData::NewL(aFieldName, aData);
94 CleanupStack::PushL(mimeData);
95 iNonStandardMimeHeaders.Append(mimeData);
96 CleanupStack::Pop(mimeData);
99 EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeField(TInt aIndex) const
101 return iNonStandardMimeHeaders[aIndex]->FieldName();
104 EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeData(TInt aIndex) const
106 return iNonStandardMimeHeaders[aIndex]->Data();
109 EXPORT_C void CCafMimeHeader::ExternalizeL(RWriteStream& aStream) const
113 // Write the contents of this class out to a stream
115 // Write the number of standard mime headers
116 aStream.WriteInt32L(EMimeMax);
118 // Write out the standard mime data, each one preceeded by it's length
119 for(i = 0; i < EMimeMax; i++)
123 // Length is zero if the mime field has not been set,
124 if(iStandardMimeHeader[i] == NULL)
130 length = iStandardMimeHeader[i]->Length();
133 // write the field length
134 aStream.WriteInt16L(length);
137 // write the field value if length > 0
138 aStream.WriteL(*iStandardMimeHeader[i]);
142 aStream.WriteInt32L(iNonStandardMimeHeaders.Count());
144 // Write out non standard mime headers and data, each Des preceeded by it's length
145 for(i = 0; i < iNonStandardMimeHeaders.Count(); i++)
147 aStream.WriteInt16L(iNonStandardMimeHeaders[i]->FieldName().Length());
148 if(iNonStandardMimeHeaders[i]->FieldName().Length() > 0)
150 aStream.WriteL(iNonStandardMimeHeaders[i]->FieldName());
152 aStream.WriteInt16L(iNonStandardMimeHeaders[i]->Data().Length());
153 if(iNonStandardMimeHeaders[i]->Data().Length() > 0)
155 aStream.WriteL(iNonStandardMimeHeaders[i]->Data());
160 EXPORT_C void CCafMimeHeader::InternalizeL(RReadStream& aStream)
169 // clear any existing headers
170 iNonStandardMimeHeaders.ResetAndDestroy();
171 for(i = 0; i < EMimeMax; i++)
173 // delete the existing data for the mime field
174 delete iStandardMimeHeader[i];
175 iStandardMimeHeader[i] = NULL;
179 fieldCount= aStream.ReadInt32L();
180 if(fieldCount != EMimeMax)
182 // something is seriously wrong, should always be EMimeMax standard headers
183 User::Leave(KErrGeneral);
186 for(i = 0; i < EMimeMax; i++)
188 // read the length from the stream
189 length = aStream.ReadInt16L();
192 // read in non zero fields, others will remain NULL
193 data = HBufC8::NewLC(length);
194 TPtr8 dataPtr = data->Des();
195 aStream.ReadL(dataPtr,length);
196 SetStandardMimeDataL(static_cast <TMimeFields>(i), dataPtr);
197 CleanupStack::PopAndDestroy(data);
201 fieldCount= aStream.ReadInt32L();
203 for(i = 0; i < fieldCount; i++)
205 // Read mime field name
206 length = aStream.ReadInt16L();
209 User::Leave(KErrCorrupt);
212 field = HBufC8::NewLC(length);
213 TPtr8 fieldPtr = field->Des();
216 aStream.ReadL(fieldPtr,length);
220 length = aStream.ReadInt16L();
223 User::Leave(KErrCorrupt);
226 data = HBufC8::NewLC(length);
227 TPtr8 dataPtr = data->Des();
230 aStream.ReadL(dataPtr,length);
233 // Add a new mime field and data to our array
234 AddNonStandardMimeL(fieldPtr, dataPtr);
236 // free memory allocated
237 CleanupStack::PopAndDestroy(data);
238 CleanupStack::PopAndDestroy(field);
243 #endif // #ifndef REMOVE_CAF1