contentmgmt/contentaccessfwfordrm/source/cafutils/cafmimeheader.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2003-2009 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef REMOVE_CAF1
       
    20 
       
    21 #include <caf/caftypes.h>
       
    22 #include <s32strm.h>
       
    23 #include "cafmimeheader.h"
       
    24 #include "mimefieldanddata.h"
       
    25 
       
    26 using namespace ContentAccess;
       
    27 
       
    28 EXPORT_C CCafMimeHeader* CCafMimeHeader::NewL(const TDesC8 &aContentType)
       
    29 	{
       
    30 	CCafMimeHeader *self = new (ELeave) CCafMimeHeader();
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL(aContentType);
       
    33 	CleanupStack::Pop(self);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CCafMimeHeader::CCafMimeHeader() 
       
    38 	{
       
    39 	}
       
    40 
       
    41 void CCafMimeHeader::ConstructL(const TDesC8 &aContentType) 
       
    42 	{
       
    43 	SetStandardMimeDataL(EContentType, aContentType);
       
    44 	}
       
    45 
       
    46 EXPORT_C CCafMimeHeader::~CCafMimeHeader()
       
    47 	{
       
    48 	TInt i=0;
       
    49 	iNonStandardMimeHeaders.ResetAndDestroy();
       
    50 	iNonStandardMimeHeaders.Close();
       
    51 	
       
    52 	for(i = 0; i < EMimeMax; i++)
       
    53 		{
       
    54 		delete iStandardMimeHeader[i];
       
    55 		}
       
    56 	}
       
    57 
       
    58 EXPORT_C void CCafMimeHeader::SetStandardMimeDataL(const TMimeFields &aIndex, const TDesC8& aData)
       
    59 	{
       
    60 	HBufC8 *data = aData.AllocLC();
       
    61 
       
    62 	// make sure mime types are lower case
       
    63 	if(aIndex == EContentType)
       
    64 		{
       
    65 		data->Des().LowerCase();
       
    66 		}
       
    67 	
       
    68 	// replace whatever value was there before
       
    69 	delete iStandardMimeHeader[aIndex];
       
    70 	iStandardMimeHeader[aIndex] = data;
       
    71 		
       
    72 	CleanupStack::Pop(data); 
       
    73 	}
       
    74 
       
    75 EXPORT_C TPtrC8 CCafMimeHeader::StandardMimeData(const TMimeFields &aIndex) const
       
    76 	{
       
    77 	// If the field value has not been set, return a null string
       
    78 	if(iStandardMimeHeader[aIndex] == NULL)
       
    79 		{
       
    80 		return KNullDesC8();
       
    81 		}
       
    82 	return TPtrC8(iStandardMimeHeader[aIndex]->Des());
       
    83 	}
       
    84 
       
    85 
       
    86 EXPORT_C TInt CCafMimeHeader::NonStandardMimeCount() const
       
    87 	{
       
    88 	return iNonStandardMimeHeaders.Count();
       
    89 	}
       
    90 
       
    91 EXPORT_C void CCafMimeHeader::AddNonStandardMimeL(const TDesC8& aFieldName, const TDesC8& aData)
       
    92 	{
       
    93 	CMimeFieldAndData *mimeData = CMimeFieldAndData::NewL(aFieldName, aData);
       
    94 	CleanupStack::PushL(mimeData);
       
    95 	iNonStandardMimeHeaders.Append(mimeData);
       
    96 	CleanupStack::Pop(mimeData);
       
    97 	}
       
    98 
       
    99 EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeField(TInt aIndex) const
       
   100 	{
       
   101 	return iNonStandardMimeHeaders[aIndex]->FieldName();
       
   102 	}
       
   103 
       
   104 EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeData(TInt aIndex) const
       
   105 	{
       
   106 	return iNonStandardMimeHeaders[aIndex]->Data();
       
   107 	}
       
   108 
       
   109 EXPORT_C void CCafMimeHeader::ExternalizeL(RWriteStream& aStream) const
       
   110 	{
       
   111 	TInt i=0;
       
   112 	
       
   113 	// Write the contents of this class out to a stream
       
   114 
       
   115 	// Write the number of standard mime headers
       
   116 	aStream.WriteInt32L(EMimeMax);
       
   117 	
       
   118 	// Write out the standard mime data, each one preceeded by it's length
       
   119 	for(i = 0; i < EMimeMax; i++)
       
   120 		{
       
   121 		TInt length=0;
       
   122 		
       
   123 		// Length is zero if the mime field has not been set, 
       
   124 		if(iStandardMimeHeader[i] == NULL)
       
   125 			{
       
   126 			length = 0;
       
   127 			}
       
   128 		else 
       
   129 			{
       
   130 			length = iStandardMimeHeader[i]->Length();
       
   131 			}
       
   132 
       
   133 		// write the field length
       
   134 		aStream.WriteInt16L(length);
       
   135 		if(length > 0)
       
   136 			{
       
   137 			// write the field value if length > 0
       
   138 			aStream.WriteL(*iStandardMimeHeader[i]);
       
   139 			}
       
   140 		}
       
   141 
       
   142 	aStream.WriteInt32L(iNonStandardMimeHeaders.Count());
       
   143 
       
   144 	// Write out non standard mime headers and data, each Des preceeded by it's length
       
   145 	for(i = 0; i < iNonStandardMimeHeaders.Count(); i++)
       
   146 		{
       
   147 		aStream.WriteInt16L(iNonStandardMimeHeaders[i]->FieldName().Length());
       
   148 		if(iNonStandardMimeHeaders[i]->FieldName().Length() > 0)
       
   149 			{
       
   150 			aStream.WriteL(iNonStandardMimeHeaders[i]->FieldName());
       
   151 			}
       
   152 		aStream.WriteInt16L(iNonStandardMimeHeaders[i]->Data().Length());
       
   153 		if(iNonStandardMimeHeaders[i]->Data().Length() > 0)
       
   154 			{
       
   155 			aStream.WriteL(iNonStandardMimeHeaders[i]->Data());
       
   156 			}
       
   157 		}
       
   158 	}
       
   159 
       
   160 EXPORT_C void CCafMimeHeader::InternalizeL(RReadStream& aStream)
       
   161 	{
       
   162 	TInt i=0;
       
   163 	TInt fieldCount=0;
       
   164 	TInt16 length=0;
       
   165 	HBufC8 *field=NULL;
       
   166 	HBufC8 *data=NULL;
       
   167 	
       
   168 
       
   169 	// clear any existing headers	
       
   170 	iNonStandardMimeHeaders.ResetAndDestroy();
       
   171 	for(i = 0; i < EMimeMax; i++)
       
   172 		{
       
   173 		// delete the existing data for the mime field
       
   174 		delete iStandardMimeHeader[i];
       
   175 		iStandardMimeHeader[i] = NULL;
       
   176 		}
       
   177 
       
   178 
       
   179 	fieldCount= aStream.ReadInt32L();
       
   180 	if(fieldCount != EMimeMax)
       
   181 		{
       
   182 		// something is seriously wrong, should always be EMimeMax standard headers
       
   183 		User::Leave(KErrGeneral);
       
   184 		}
       
   185 	
       
   186 	for(i = 0; i < EMimeMax; i++)
       
   187 		{
       
   188 		// read the length from the stream
       
   189 		length = aStream.ReadInt16L();
       
   190 		if(length > 0)
       
   191 			{
       
   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);
       
   198 			}
       
   199 		}
       
   200 
       
   201 	fieldCount= aStream.ReadInt32L();
       
   202 
       
   203 	for(i = 0; i < fieldCount; i++)
       
   204 		{
       
   205 		// Read mime field name
       
   206 		length = aStream.ReadInt16L();
       
   207 		if(length < 0)
       
   208 			{
       
   209 			User::Leave(KErrCorrupt);
       
   210 			}
       
   211 
       
   212 		field = HBufC8::NewLC(length);
       
   213 		TPtr8 fieldPtr = field->Des();
       
   214 		if(length > 0)
       
   215 			{
       
   216 			aStream.ReadL(fieldPtr,length);
       
   217 			}
       
   218 		
       
   219 		// Read data
       
   220 		length = aStream.ReadInt16L();
       
   221 		if(length < 0)
       
   222 			{
       
   223 			User::Leave(KErrCorrupt);
       
   224 			}
       
   225 
       
   226 		data = HBufC8::NewLC(length);
       
   227 		TPtr8 dataPtr = data->Des();
       
   228 		if(length > 0)
       
   229 			{
       
   230 			aStream.ReadL(dataPtr,length);
       
   231 			}
       
   232 
       
   233 		// Add a new mime field and data to our array
       
   234 		AddNonStandardMimeL(fieldPtr, dataPtr);
       
   235 
       
   236 		// free memory allocated
       
   237 		CleanupStack::PopAndDestroy(data);
       
   238 		CleanupStack::PopAndDestroy(field); 
       
   239 		}
       
   240 	}
       
   241 
       
   242 
       
   243 #endif // #ifndef REMOVE_CAF1