email/imap4mtm/imapsession/src/cimapheaderfields.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cimapheaderfields.h"
       
    17 
       
    18 CImapHeaderFields::CImapHeaderFields()
       
    19 	{
       
    20 	}
       
    21 
       
    22 void CImapHeaderFields::ConstructL(TInt aStartFieldCount)
       
    23 	{
       
    24 	for (TInt field = 0; field < aStartFieldCount; ++field)
       
    25 		{
       
    26 		iImapFieldValueArray.AppendL(NULL);
       
    27 		}
       
    28 	}
       
    29 
       
    30 CImapHeaderFields::~CImapHeaderFields()
       
    31 	{
       
    32 	iImapFieldValueArray.ResetAndDestroy();
       
    33 	}
       
    34 
       
    35 /**
       
    36 Sets the value of the field identified by the supplied field name
       
    37 @param aFieldname Name of the field to set
       
    38 @param aValue Value to set the field to. This routine takes immediate
       
    39        ownership of the buffer
       
    40 @return Whether field has been set to the new value
       
    41 */
       
    42 TBool CImapHeaderFields::SetFieldL(const TDesC8& aFieldName, HBufC8* aValue)
       
    43 	{
       
    44 	CleanupStack::PushL(aValue);
       
    45 
       
    46 	TBool needToStripSpaces = EFalse;
       
    47 
       
    48 	TInt fieldId = Match(aFieldName, needToStripSpaces);
       
    49 
       
    50 	if (fieldId != KErrNotFound)
       
    51 		{
       
    52 		ASSERT(fieldId < iImapFieldValueArray.Count());
       
    53 
       
    54 		if (needToStripSpaces)
       
    55 			{
       
    56 			HBufC8* strippedValue = StripSpacesL(*aValue);
       
    57 			CleanupStack::PopAndDestroy(aValue);
       
    58 			aValue = strippedValue;
       
    59 			CleanupStack::PushL(aValue);
       
    60 			}
       
    61 
       
    62 		delete iImapFieldValueArray[fieldId];
       
    63 		iImapFieldValueArray[fieldId] = aValue;
       
    64 		CleanupStack::Pop(aValue);
       
    65 		return ETrue;
       
    66 		}
       
    67 
       
    68 	CleanupStack::PopAndDestroy(aValue);
       
    69 	return EFalse;
       
    70 	}
       
    71 
       
    72 /**
       
    73 Returns the value of the field identified by the supplied field index.
       
    74 If the field has not been populated, a null descriptor is returned.
       
    75 @param aFieldId index of the field value to return.
       
    76 @return The value of the filed or a null descriptor.
       
    77 */
       
    78 const TDesC8& CImapHeaderFields::FieldValue(TInt aFieldId)
       
    79 	{
       
    80 	ASSERT(aFieldId < iImapFieldValueArray.Count());
       
    81 
       
    82 	return (iImapFieldValueArray[aFieldId] != NULL) ? *(iImapFieldValueArray[aFieldId]) : KNullDesC8();
       
    83 	}
       
    84 
       
    85 /**
       
    86 Returns whether the field identified by the supplied field index has been populated.
       
    87 This indicates whether the field value was supplied by the server.
       
    88 @param aFieldId index of the field value to return.
       
    89 @return Whether field has been populated.
       
    90 */
       
    91 TBool CImapHeaderFields::FieldExists(TInt aFieldId)
       
    92 	{
       
    93 	ASSERT(aFieldId < iImapFieldValueArray.Count());
       
    94 
       
    95 	return (iImapFieldValueArray[aFieldId] != NULL) ? ETrue : EFalse;
       
    96 	}
       
    97 
       
    98 /**
       
    99 Strips all spaces from a header field
       
   100 @param aValue Field value to strip spaces from
       
   101 @return Buffer containing stripped value. Ownership is passed back to caller
       
   102 */
       
   103 HBufC8* CImapHeaderFields::StripSpacesL(const TDesC8& aBuffer)
       
   104 	{
       
   105 	TInt bufferLen = aBuffer.Length();
       
   106 
       
   107 	HBufC8* strippedValue = HBufC8::NewL(bufferLen);
       
   108 	TPtr8 strippedBuffer = strippedValue->Des();
       
   109 
       
   110 	for (TInt bufferPos = 0; bufferPos < bufferLen; ++bufferPos)
       
   111 		{
       
   112 		if (aBuffer[bufferPos] > ' ')
       
   113 			{
       
   114 			strippedBuffer.Append(aBuffer[bufferPos]);
       
   115 			}
       
   116 		}
       
   117 
       
   118 	return strippedValue;
       
   119 	}