messagingfw/wappushfw/tpushscriptbased/wappushmsggen.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2007-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 
       
    17 #include "wappushmsggen.h"
       
    18 
       
    19 _LIT8(KXmlVersion, "<?xml version=\"1.0\"?>\r\n");
       
    20 _LIT8(KBadXmlVersion, "<?xml version=\"2.0\"?>\r\n");  // Unsupported XML version
       
    21 _LIT8(KSiStart, "<!DOCTYPE si PUBLIC \"-//WAPFORUM//DTD SI 1.0//EN\"\r\n\"http://www.wapforum.org/DTD/si.dtd\">\r\n<si>\r\n<indication ");
       
    22 _LIT8(KSiEnd, "</indication>\r\n</si>\r\n");
       
    23 _LIT8(KSlStart, "<!DOCTYPE sl PUBLIC \"-//WAPFORUM//DTD SL 1.0//EN\"\r\n\"http://www.wapforum.org/DTD/sl.dtd\">\r\n<sl ");
       
    24 _LIT8(KSlEnd, ">\r\n</sl>\r\n");
       
    25 
       
    26 // Valid XML tags and data
       
    27 _LIT8(KHRefField, "href=\"");
       
    28 _LIT8(KSiIdField, "si-id=\"");
       
    29 _LIT8(KSiCreatedField, "created=\"");
       
    30 _LIT8(KSiExpiresField, "si-expires=\"");
       
    31 _LIT8(KActionField, "action=\"");
       
    32 
       
    33 
       
    34 //Invalid XML tags & data 
       
    35 _LIT8(KBadHRefField, "hreg=\"");
       
    36 _LIT8(KBadSiIdField, "si-eyed=\"");
       
    37 _LIT8(KBadSiCreatedField, "crated=\"");
       
    38 _LIT8(KBadSiExpiresField, "si-xpires=\"");
       
    39 _LIT8(KBadActionField, "adtion=\"");
       
    40 
       
    41 
       
    42 _LIT8(KCrLf, "\r\n");
       
    43 _LIT8(KDoubleQuote,"\"");
       
    44 
       
    45 /** amount used to lengthen the buffer when appending data to it that would otherwise overflow it
       
    46 */
       
    47 const TInt KMsgBufferSegmentLength=256;
       
    48 
       
    49 /** public constructor */
       
    50 CWapPushMsgGen* CWapPushMsgGen::NewL()
       
    51 	{
       
    52 	CWapPushMsgGen* thisptr = new(ELeave) CWapPushMsgGen();
       
    53 	return thisptr;
       
    54 	}
       
    55 
       
    56 
       
    57 /** destructor */
       
    58 CWapPushMsgGen::~CWapPushMsgGen()
       
    59 	{
       
    60 	delete iMsgBuffer;
       
    61 	delete iMsgFieldBuffer;
       
    62 	}
       
    63 	
       
    64 
       
    65 /** appends a field to the end of the current message being generated
       
    66  @param aField	The type of field to be set
       
    67  @param aFieldValue The value of the field to be set. This should not 
       
    68  include CR/LF at the end for fields included in " "
       
    69  @leave HBufC8::NewL
       
    70  @leave HBufC8::ReAllocL
       
    71  @return void
       
    72 */
       
    73 void CWapPushMsgGen::AppendFieldL(TMsgField aFieldType, const TDesC8& aFieldValue)
       
    74 	{
       
    75 	if (!iMsgFieldBuffer)
       
    76 		iMsgFieldBuffer=HBufC8::NewL(KMsgBufferSegmentLength);
       
    77 
       
    78 	switch (aFieldType)
       
    79 		{
       
    80 	case EHRef:
       
    81 		AppendMsgFieldDesL(KHRefField);
       
    82 		break;
       
    83 	case EServiceIndicationID:
       
    84 		AppendMsgFieldDesL(KSiIdField);
       
    85 		break;
       
    86 	case EServiceIndicationCreated:
       
    87 		AppendMsgFieldDesL(KSiCreatedField);
       
    88 		break;
       
    89 	case EServiceIndicationExpires:
       
    90 		AppendMsgFieldDesL(KSiExpiresField);
       
    91 		break;
       
    92 	case EServiceIndicationText:
       
    93 		{
       
    94 		_LIT8(KEndIndication,">\r\n");
       
    95 		AppendMsgFieldDesL(KEndIndication);
       
    96 		}
       
    97 		break;
       
    98 	case EAction:
       
    99 		AppendMsgFieldDesL(KActionField);
       
   100 		break;
       
   101 
       
   102 	case EBadHRef:
       
   103 		AppendMsgFieldDesL(KBadHRefField);
       
   104 		break;
       
   105 	case EBadServiceIndicationID:
       
   106 		AppendMsgFieldDesL(KBadSiIdField);
       
   107 		break;
       
   108 	case EBadServiceIndicationCreated:
       
   109 		AppendMsgFieldDesL(KBadSiCreatedField);
       
   110 		break;
       
   111 	case EBadServiceIndicationExpires:
       
   112 		AppendMsgFieldDesL(KBadSiExpiresField);
       
   113 		break;
       
   114 	case EBadAction:
       
   115 		AppendMsgFieldDesL(KBadActionField);
       
   116 		break;
       
   117 	case EXMLNoFieldStr:
       
   118 	case EWBXMLNoFieldStr:
       
   119 		//Present so that can create an empty body using the msg Gen
       
   120 		break;
       
   121 	default:
       
   122 		Panic(EInvalidPushMessageField);
       
   123 		}
       
   124 
       
   125 	AppendMsgFieldDesL(aFieldValue);
       
   126 	//if (iMsgType != EServiceIndicationText)
       
   127 	if (aFieldType != EServiceIndicationText  && aFieldType != EXMLNoFieldStr  
       
   128 						&& aFieldType != EWBXMLNoFieldStr)
       
   129 		AppendMsgFieldDesL(KDoubleQuote);
       
   130 	if (aFieldType!= EWBXMLNoFieldStr)
       
   131 		AppendMsgFieldDesL(KCrLf);
       
   132 	}
       
   133 
       
   134 
       
   135 /** closes the current message and returns the whole generated message
       
   136  @return returns a dynamic descriptor that is on the cleanup stack
       
   137  @leave HBufC8::NewLC 
       
   138 */	
       
   139 HBufC8* CWapPushMsgGen::CreateMsgLC()
       
   140 	{
       
   141 	TInt size = (*iMsgBuffer).Length() + (*iMsgFieldBuffer).Length();
       
   142 	switch(iMsgType)
       
   143 		{
       
   144 	case EServiceIndication:
       
   145 		size+= KSiEnd().Length();
       
   146 		break;
       
   147 	case EServiceLoad:
       
   148 		size+=KSlEnd().Length();
       
   149 		break;
       
   150 	case ECorruptMsgTest:
       
   151 	case ECorruptXmlVersionTest:
       
   152 	case EEmptyMsgBody:
       
   153 		// No action - don't add anything extra to the body
       
   154 		break;
       
   155 	default:
       
   156 		Panic(EInvalidPushMessageType);
       
   157 		};
       
   158 
       
   159 	HBufC8* finalMsg=HBufC8::NewLC(size);
       
   160 	TPtr8 finalMsgPtr = finalMsg->Des();
       
   161 	finalMsgPtr.Append(*iMsgBuffer);
       
   162 	finalMsgPtr.Append(*iMsgFieldBuffer);
       
   163 
       
   164 	switch(iMsgType)
       
   165 		{
       
   166 	case EServiceIndication:
       
   167 		finalMsgPtr.Append(KSiEnd);
       
   168 		break;
       
   169 	case EServiceLoad:
       
   170 		finalMsgPtr.Append(KSlEnd);
       
   171 		break;
       
   172 	case ECorruptMsgTest:
       
   173 	case ECorruptXmlVersionTest:
       
   174 	case EEmptyMsgBody:
       
   175 		// Do nothing  - leave body as is
       
   176 		break;
       
   177 	default:
       
   178 		Panic(EInvalidPushMessageType);
       
   179 		}
       
   180 	return finalMsg;
       
   181 }
       
   182 
       
   183 
       
   184 /** starts a new message 
       
   185  This starts a new message by clearing the current one. It then appends the
       
   186  header for the type of message being generated
       
   187 
       
   188   @param aMsgType an enum of type TMsgType specifing the type of message to be generated
       
   189 */	
       
   190 void CWapPushMsgGen::StartNewMsgL(TMsgType aMsgType)
       
   191 	{
       
   192 	delete iMsgBuffer;
       
   193 	iMsgBuffer = NULL;
       
   194 	delete iMsgFieldBuffer;
       
   195 	iMsgFieldBuffer = NULL;
       
   196 	iMsgType = aMsgType;
       
   197 	iMsgBuffer = HBufC8::NewL(KMsgBufferSegmentLength);
       
   198 
       
   199 	
       
   200 	switch (iMsgType)
       
   201 		{
       
   202 	case EServiceIndication:
       
   203 		AppendMsgDesL(KXmlVersion);
       
   204 		AppendMsgDesL(KSiStart);
       
   205 		break;
       
   206 	case EServiceLoad:
       
   207 		AppendMsgDesL(KXmlVersion);
       
   208 		AppendMsgDesL(KSlStart);
       
   209 		break;
       
   210 	case ECorruptMsgTest:
       
   211 		AppendMsgDesL(KXmlVersion);
       
   212 		break;
       
   213 	case ECorruptXmlVersionTest:
       
   214 		AppendMsgDesL(KBadXmlVersion);
       
   215 		break;
       
   216 	case EEmptyMsgBody:
       
   217 		// do nothing - all we want is a Null Descriptor
       
   218 		break;
       
   219 	default:
       
   220 		Panic(EInvalidPushMessageType);
       
   221 		}
       
   222 	}
       
   223 
       
   224 
       
   225 /** used to append a descriptor to the current message buffer
       
   226  @param aDes, the descriptor to be added to the message buffer
       
   227 */
       
   228 void CWapPushMsgGen::AppendMsgDesL(const TDesC8& aDes)
       
   229 	{
       
   230 	TInt maxLength = iMsgBuffer->Des().MaxLength();
       
   231 	if (maxLength < aDes.Length() + (*iMsgBuffer).Length())
       
   232 		{
       
   233 		TInt newMaxLength = maxLength;
       
   234 		newMaxLength += aDes.Length() > KMsgBufferSegmentLength? aDes.Length() : KMsgBufferSegmentLength;
       
   235 		iMsgBuffer = iMsgBuffer->ReAllocL(newMaxLength);
       
   236 		}
       
   237 	iMsgBuffer->Des().Append(aDes);
       
   238 	}
       
   239 
       
   240 
       
   241 /**  used to append a descriptor to the current message field buffer 
       
   242  @param aDes, the descriptor to be added to the message buffer
       
   243 */
       
   244 void CWapPushMsgGen::AppendMsgFieldDesL(const TDesC8& aDes)
       
   245 	{
       
   246 	TInt maxLength = iMsgFieldBuffer->Des().MaxLength();
       
   247 	if (maxLength < aDes.Length() + (*iMsgFieldBuffer).Length())
       
   248 		{
       
   249 		TInt newMaxLength = maxLength;
       
   250 		newMaxLength += aDes.Length() > KMsgBufferSegmentLength? aDes.Length() : KMsgBufferSegmentLength;
       
   251 		iMsgFieldBuffer = iMsgFieldBuffer->ReAllocL(newMaxLength);
       
   252 		}
       
   253 	iMsgFieldBuffer->Des().Append(aDes);
       
   254 	}
       
   255 
       
   256 /** Panic function
       
   257 */
       
   258 void CWapPushMsgGen::Panic(TWapPushMsgGenPanic aPanicCode)
       
   259 	{
       
   260 	_LIT(KWapPushMsgGenName,"PushMsgGen");
       
   261 	User::Panic(KWapPushMsgGenName, aPanicCode);
       
   262 	}