omads/omadsextensions/adapters/sms/src/vmessageparser.cpp
changeset 40 b63e67867dcd
child 64 a62b67d1f67c
equal deleted inserted replaced
39:9905f7d46607 40:b63e67867dcd
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Part of SyncML Data Synchronization Plug In Adapter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32std.h>
       
    21 #include <msvids.h>
       
    22 #include <txtrich.h>
       
    23 #include "vmessageparser.h"
       
    24 #include "logger.h"
       
    25 
       
    26 
       
    27 const TInt KVersionMaxLength = 4;
       
    28 const TInt KDateMaxLength = 48;
       
    29 const TInt KTagAndValueMaxLength = 48;
       
    30 const TInt KDefaultWriteBufSize = 1024;
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CVMessageParser* CVMessageParser::NewL()
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CVMessageParser* CVMessageParser::NewL()
       
    39 	{
       
    40 	CVMessageParser* self = NewLC();
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43 	}
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CVMessageParser* CVMessageParser::NewLC()
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CVMessageParser* CVMessageParser::NewLC()
       
    50 	{
       
    51 	CVMessageParser* self = new( ELeave ) CVMessageParser();
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     return self;
       
    55 	}
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CVMessageParser::~CVMessageParser()
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CVMessageParser::~CVMessageParser()
       
    62 	{
       
    63 	iRecipients.Close();
       
    64 	delete iMessageBody;
       
    65 	iMessageBody = NULL;
       
    66 	}
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CVMessageParser::CVMessageParser()
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CVMessageParser::CVMessageParser()
       
    73 	{
       
    74 	}
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CVMessageParser::ConstructL()
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CVMessageParser::ConstructL()
       
    81 	{
       
    82 	}
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CVMessageParser::ReadMessage()
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 TInt CVMessageParser::ParseMessageL( const TDesC8& aVMessage )
       
    89 	{
       
    90 	LOGGER_ENTERFN( "CVMessageParser::ParseMessageL" );
       
    91 
       
    92 	const TUint8* ptr8 = aVMessage.Ptr();	
       
    93 	const TUint16* ptr16 = reinterpret_cast<const TUint16*>( ptr8 );
       
    94 	iReadBuf.Set( ptr16, aVMessage.Length()/2 );
       
    95 	LOG(iReadBuf);	
       
    96 	
       
    97 	iReadBufPosition = 0;
       
    98 
       
    99 	TInt err = ReadMessageHeader();
       
   100 	
       
   101 	if (err < 0)
       
   102 		{
       
   103 		LOGGER_WRITE_1( "ReadMessageHeader() failed with %d", err );
       
   104 		}
       
   105 	else
       
   106 		{
       
   107 		err = ReadMessageBodyL();
       
   108 		LOGGER_WRITE_1( "ReadMessageBody() result: %d", err );
       
   109 		}
       
   110     LOGGER_LEAVEFN( "CVMessageParser::ParseMessageL" );
       
   111 	return err;
       
   112 	}
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CVMessageParser::ConstructMessageL()
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CVMessageParser::ConstructMessageL( CBufBase& aVMessage )
       
   119 	{
       
   120 	LOGGER_ENTERFN( "CVMessageParser::ConstructMessageL" );
       
   121 	LOGGER_WRITE_1( "Initial buffer size: %d", aVMessage.Size() );
       
   122 
       
   123 	iWriteBuf = &aVMessage;
       
   124 	iWriteBufPosition = 0,
       
   125 	iWriteIndentLevel = 0;
       
   126 
       
   127 	iWriteBufSize = KDefaultWriteBufSize;
       
   128 	if (iMessageBody)
       
   129 		{
       
   130 		iWriteBufSize += iMessageBody->Size();
       
   131 		}
       
   132 
       
   133 	iWriteBuf->ResizeL( iWriteBufSize );
       
   134 	LOGGER_WRITE_1( "Buffer size resized to %d", iWriteBuf->Size() );
       
   135 	
       
   136 	WriteMessageLineL( KVMsgTagBegin, KVMsgSectionVMsg ); // BEGIN:VMSG
       
   137 	WriteMessagePropertiesL();
       
   138 	WriteMessageVCARDL( iSender.iName, iSender.iNumber );
       
   139 	WriteMessageEnvelopeL();
       
   140 	WriteMessageLineL( KVMsgTagEnd, KVMsgSectionVMsg ); // END:VMSG
       
   141 
       
   142 	iWriteBuf->ResizeL( iWriteBufPosition );
       
   143 
       
   144 	LOGGER_WRITE_1( "Message length: %d", iWriteBufPosition );
       
   145 	LOGGER_LEAVEFN( "CVMessageParser::ConstructMessageL" );
       
   146 	}
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CVMessageParser::ResetAll()
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CVMessageParser::ResetAll()
       
   153 	{
       
   154 	// Public data 
       
   155 
       
   156 	iHomeTime = 0;
       
   157 	iUniversalTime = 0;
       
   158 	iFolder.Zero();
       
   159 	iStatus.Zero();
       
   160 	iSender.iNumber.Zero();
       
   161 	iSender.iName.Zero();
       
   162 	iRecipients.Reset();
       
   163 	if (iMessageBody)
       
   164 		{
       
   165 		delete iMessageBody;
       
   166 		iMessageBody = NULL;
       
   167 		}
       
   168 
       
   169 
       
   170 	// Private variables
       
   171 	iReadBufPosition = 0;
       
   172 	iWriteBufPosition = 0;
       
   173 	iWriteIndentLevel = 0;
       
   174 	iWriteBuf = NULL;
       
   175 	}
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CVMessageParser::LoadMessageBodyL()
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 void CVMessageParser::LoadMessageBodyL( CRichText &aMsgBody )
       
   182 	{
       
   183 	if (iMessageBody)
       
   184 		{
       
   185 		aMsgBody.InsertL(0, *iMessageBody);
       
   186 		}
       
   187 	}
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CVMessageParser::StoreMessageBodyL()
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CVMessageParser::StoreMessageBodyL( CRichText &aMsgBody )
       
   194 	{
       
   195 	if (iMessageBody)
       
   196 		{
       
   197 		LOGGER_WRITE( "Message body not empty" );
       
   198 		User::Leave( KErrGeneral );
       
   199 		}
       
   200 
       
   201 	TInt bodyLenght =  aMsgBody.DocumentLength();
       
   202 
       
   203 	if (bodyLenght > 0)
       
   204 		{
       
   205 		iMessageBody = HBufC::NewL( bodyLenght );
       
   206 		TPtr16 ptrBody = iMessageBody->Des();
       
   207 		aMsgBody.Extract( ptrBody );
       
   208 		}
       
   209 	}
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CVMessageParser::ParseTelephoneNumber()
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TBool CVMessageParser::ParseTelephoneNumber( const TDesC &aNumberString, TTelephoneNumber &aNumberStore )
       
   216 	{
       
   217 	_LIT( KStartMarker, "<" );
       
   218 	_LIT( KEndMarker, ">" );
       
   219     LOGGER_WRITE_1("aNumberString: %S", &aNumberString );
       
   220 	TInt startMarker = aNumberString.Find( KStartMarker );
       
   221 
       
   222 	if (startMarker >= 0)
       
   223 		{
       
   224 		TInt endMarker = aNumberString.Find( KEndMarker );
       
   225 		if (endMarker <= (startMarker + 1))
       
   226 			{
       
   227 			LOGGER_WRITE( "Incorrect number" );
       
   228 			return EFalse;
       
   229 			}
       
   230 		else // case 1: Abc Pqr <+01234567890>
       
   231 			{
       
   232 			TPtrC16 name = aNumberString.Left( startMarker );
       
   233 			TPtrC16 number = aNumberString.Mid( (startMarker + 1), (endMarker - startMarker - 1) ); 
       
   234 			AssignDataToBuffer( name, aNumberStore.iName );
       
   235 			AssignDataToBuffer( number, aNumberStore.iNumber );
       
   236 			}
       
   237 		}
       
   238 	else // just number
       
   239 		{
       
   240 		AssignDataToBuffer( aNumberString, aNumberStore.iNumber );
       
   241 		aNumberStore.iName.Zero();
       
   242 		}
       
   243     LOGGER_WRITE_1("aNumberStore.iName: %S", &aNumberStore.iName);
       
   244 
       
   245 	return ETrue;
       
   246 	}
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CVMessageParser::ReadMessageHeader()
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 TInt CVMessageParser::ReadMessageHeader()
       
   253 	{
       
   254 	LOGGER_WRITE( "CVMessageParser::ReadMessageHeader()" );
       
   255 
       
   256 	TInt err;
       
   257 
       
   258 	err = FindMessageTagWithValue( KVMsgTagBegin, KVMsgSectionVMsg );
       
   259 	if (err < 0)
       
   260 		{
       
   261 		return err;
       
   262 		}
       
   263 		
       
   264 	err = ReadTaggedTimeStamp( iUniversalTime );
       
   265 	if ( err == KErrNone )
       
   266 		{
       
   267 		LOGGER_WRITE( "Time stamp tag found, universal time set" );
       
   268 		}		
       
   269 				
       
   270 	err = GetMessageTagValue( KVMsgTagStatus, iStatus );
       
   271 	if (err < 0)
       
   272 		{
       
   273 		return err;
       
   274 		}		
       
   275 	err = GetMessageTagValue( KVMsgTagBox, iFolder );
       
   276 	if (err < 0)
       
   277 		{
       
   278 		return err;
       
   279 		}
       
   280 	err = FindMessageTagWithValue( KVMsgTagBegin, KVMsgSectionVCard );
       
   281 	if (err < 0)
       
   282 		{
       
   283 		return err;
       
   284 		}
       
   285 	err = ReadVCard( iSender );
       
   286 	if (err < 0)
       
   287 		{
       
   288 		return err;
       
   289 		}
       
   290 	err = FindMessageTagWithValue( KVMsgTagBegin, KVMsgSectionVEnv );
       
   291 	if (err < 0)
       
   292 		{
       
   293 		return err;
       
   294 		}
       
   295 	else
       
   296 		{
       
   297 		err = ReadEnvelope();
       
   298 		}
       
   299 
       
   300 	return err;
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CVMessageParser::ReadEnvelope()
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 TInt CVMessageParser::ReadEnvelope()
       
   308 	{
       
   309 	TBuf<KTagAndValueMaxLength> tagValue;
       
   310 	TInt err( KErrNone );
       
   311 
       
   312 	err = GetMessageTagValue( KVMsgTagBegin, tagValue );
       
   313 	if (err < 0)
       
   314 		{
       
   315 		return err;
       
   316 		}
       
   317 	else if (tagValue.Compare( KVMsgSectionVCard ) == 0)
       
   318 		{
       
   319 		TTelephoneNumber recipient;
       
   320 		err = ReadVCard( recipient );
       
   321 		if (err < 0)
       
   322 			{
       
   323 			return err;
       
   324 			}
       
   325 		else
       
   326 			{
       
   327 			if (recipient.iNumber.Length() > 0)
       
   328 			    {
       
   329 			    iRecipients.Append( recipient );
       
   330 			    }
       
   331 			    
       
   332 			err = FindMessageTagWithValue( KVMsgTagBegin, KVMsgSectionVEnv );
       
   333 			if (err >= 0)
       
   334 				{
       
   335 				err = ReadEnvelope();
       
   336 				}
       
   337 			}
       
   338 		}
       
   339 	else if (tagValue.Compare( KVMsgSectionVBody ) == 0)
       
   340 		{
       
   341 		err = KErrNone;
       
   342 		}
       
   343 	else
       
   344 		{
       
   345 		LOGGER_WRITE( "Unexpected message tag value" );
       
   346 		return KErrGeneral;
       
   347 		}
       
   348 
       
   349 	return err;
       
   350 	}
       
   351 	
       
   352 // -----------------------------------------------------------------------------
       
   353 // CVMessageParser::ReadTaggedTimestamp()
       
   354 // Read and parse tagged timestamp from vMessage (optional feature)
       
   355 // Format: X-NOK-DT:yyyymmddThhmmssZ (example: X-NOK-DT:20060329T091530Z)
       
   356 // -----------------------------------------------------------------------------
       
   357 //	
       
   358 TInt CVMessageParser::ReadTaggedTimeStamp( TTime& aUniversalTime )
       
   359 	{
       
   360 	LOGGER_WRITE( "CVMessageParser::ReadTaggedTimestamp" );	
       
   361 	
       
   362 	TInt err( KErrNone );
       
   363 	TBuf<20> timeStamp;
       
   364 	
       
   365 	err = GetMessageTagValue( KVMsgTagDateTime, timeStamp, EFalse );
       
   366 	if ( err < 0 )
       
   367 		{
       
   368 		return KErrNotFound;
       
   369 		}
       
   370 	
       
   371 	LOG( timeStamp );
       
   372 	
       
   373 	if (timeStamp.Length() != 16 || timeStamp[8] != 'T' || timeStamp[15] != 'Z')
       
   374 		{
       
   375 		return KErrNotSupported;
       
   376 		}
       
   377 
       
   378 	TLex lexYear( timeStamp.Mid( 0, 4 ) );
       
   379 	TLex lexMonth( timeStamp.Mid( 4, 2 ) );
       
   380 	TLex lexDay( timeStamp.Mid( 6, 2 ) );
       
   381 	TLex lexHour( timeStamp.Mid( 9, 2 ) );
       
   382 	TLex lexMinute( timeStamp.Mid( 11, 2 ) );
       
   383 	TLex lexSecond( timeStamp.Mid( 13, 2 ) );		
       
   384 	
       
   385 	TInt valYear( 0 );
       
   386 	TInt valMonth( 0 );
       
   387 	TInt valDay( 0 );
       
   388 	TInt valHour( 0 );
       
   389 	TInt valMinute( 0 );
       
   390 	TInt valSecond( 0 );
       
   391 
       
   392 	TBool parseDone( EFalse );		
       
   393 	if ( lexYear.Val( valYear ) == KErrNone )
       
   394 		{
       
   395 		if ( lexMonth.Val( valMonth ) == KErrNone )
       
   396 			{
       
   397 			if ( lexDay.Val( valDay ) == KErrNone )
       
   398 				{
       
   399 				if ( lexHour.Val( valHour ) == KErrNone )
       
   400 					{
       
   401 					if ( lexMinute.Val( valMinute ) == KErrNone )
       
   402 						{
       
   403 						if ( lexSecond.Val( valSecond ) == KErrNone )
       
   404 							{
       
   405 							parseDone = ETrue;
       
   406 							}
       
   407 						}
       
   408 					}
       
   409 				}
       
   410 			}		
       
   411 		}
       
   412 	if ( !parseDone )
       
   413 		{
       
   414 		return KErrNotSupported;
       
   415 		}
       
   416 		
       
   417 	TDateTime dateTime;
       
   418 	TMonth month = static_cast<TMonth>(--valMonth);
       
   419 	err = dateTime.Set( valYear, month, --valDay, valHour, valMinute, valSecond, 0 );	
       
   420 	if ( err != KErrNone )
       
   421 		{
       
   422 		return KErrNotSupported;
       
   423 		}
       
   424 		
       
   425 	aUniversalTime = dateTime;	
       
   426 	
       
   427 	return KErrNone; 	
       
   428 	}		
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CVMessageParser::ReadVCard()
       
   432 // -----------------------------------------------------------------------------
       
   433 //
       
   434 TInt CVMessageParser::ReadVCard( TTelephoneNumber &aResult )
       
   435 	{		
       
   436 	TInt err = KErrNone;
       
   437 	TBuf<KVersionMaxLength> version;
       
   438 
       
   439 	err = GetMessageTagValue( KVMsgTagVersion, version );
       
   440 	if (err < 0)
       
   441 		{
       
   442 		return err;
       
   443 		}
       
   444 
       
   445 	err = GetMessageTagValue( KVMsgTagName, aResult.iName );
       
   446 	if (err < 0)
       
   447 		{
       
   448 		return err;
       
   449 		}
       
   450 
       
   451 	err = GetMessageTagValue( KVMsgTagTelephone, aResult.iNumber );
       
   452 	if (err < 0)
       
   453 		{
       
   454 		return err;
       
   455 		}
       
   456 
       
   457 	err = FindMessageTagWithValue( KVMsgTagEnd, KVMsgSectionVCard );
       
   458 	if (err < 0)
       
   459 		{
       
   460 		return err;
       
   461 		}
       
   462 
       
   463 	return KErrNone;
       
   464 	}
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CVMessageParser::ReadMessageBody()
       
   468 // -----------------------------------------------------------------------------
       
   469 //
       
   470 TInt CVMessageParser::ReadMessageBodyL()
       
   471 	{
       
   472 	LOGGER_WRITE( "CVMessageParser::ReadMessageBody()" );
       
   473 
       
   474 	TInt err;
       
   475 	
       
   476 	TPtrC pBuf = iReadBuf.Mid( iReadBufPosition );
       
   477 	err = pBuf.Find( KVMsgLineFeed );
       
   478 	if (err < 0)
       
   479 		{
       
   480 		return err;
       
   481 		}
       
   482 	
       
   483 	iReadBufPosition += ( err + KVMsgLineFeed().Length() );
       
   484 	TInt bodyStart = iReadBufPosition;
       
   485 
       
   486 	TBuf<KDateMaxLength> dateBuf;
       
   487 	err = GetMessageTagValue(KVMsgTagDate, dateBuf);
       
   488 	if (err < 0)
       
   489 		{
       
   490 		LOGGER_WRITE( "No date field in message body" );
       
   491 		iReadBufPosition = bodyStart;
       
   492 		}
       
   493 	else
       
   494 		{
       
   495 		for (TInt i = 0; i < dateBuf.Length(); i++)
       
   496 			{
       
   497 			// This is needed, because TTime::Parse() can not handle
       
   498 			// situation where '.' character is used as a delimiter in date.
       
   499 			// Expected date format is like 18.1.2005 17:32:50.
       
   500 			//
       
   501 			if (dateBuf[i] == ' ')
       
   502 				{
       
   503 				break;
       
   504 				}
       
   505 			else if (dateBuf[i] == '.')
       
   506 				{
       
   507 				dateBuf[i] = '/';
       
   508 				}
       
   509 			}
       
   510 		
       
   511         TLocale locale;
       
   512         TDateFormat originalFormat = locale.DateFormat();
       
   513         locale.SetDateFormat( EDateEuropean );
       
   514         locale.Set(); // transfer the new locale settings to the system
       
   515         
       
   516 		err = iHomeTime.Parse( dateBuf );
       
   517 		if ( err < 0 )
       
   518 			{
       
   519 			LOGGER_WRITE_1( "Parsing date from message body failed with %d", err );
       
   520 			iHomeTime = 0;
       
   521 			}
       
   522 		locale.SetDateFormat( originalFormat );
       
   523 		locale.Set(); // transfer the original locale settings back to the system
       
   524 
       
   525 		TPtrC pDateStart = iReadBuf.Mid( iReadBufPosition );
       
   526 		err = pDateStart.Find( KVMsgLineFeed );
       
   527 		if (err < 0)
       
   528 			{
       
   529 			return err;
       
   530 			}
       
   531 		iReadBufPosition += ( err + KVMsgLineFeed().Length() );
       
   532 		bodyStart = iReadBufPosition;
       
   533 		}
       
   534 
       
   535 	err = FindMessageTagWithValue( KVMsgTagEnd, KVMsgSectionVBody );
       
   536 	if (err < 0)
       
   537 		{
       
   538 		return err;
       
   539 		}
       
   540 	else
       
   541 		{
       
   542 		TInt bodyLength = err - bodyStart;
       
   543 		iMessageBody = HBufC::NewL( bodyLength );
       
   544 		TPtrC pData = iReadBuf.Mid( bodyStart, bodyLength );
       
   545 		TPtr pMsgBody = iMessageBody->Des();
       
   546 		pMsgBody.Copy( pData );
       
   547 		pMsgBody.Trim();
       
   548 		err = pMsgBody.Length();
       
   549 		LOG( pMsgBody );
       
   550 		}
       
   551 
       
   552 	return err;
       
   553 	}
       
   554 
       
   555 // -----------------------------------------------------------------------------
       
   556 // CVMessageParser::FindMessageTagWithValue()
       
   557 // -----------------------------------------------------------------------------
       
   558 //
       
   559 TInt CVMessageParser::FindMessageTagWithValue( const TDesC& aMsgTag, const TDesC& aValue )
       
   560 	{
       
   561 	TBuf<KTagAndValueMaxLength> tagAndValue( aMsgTag );
       
   562 	tagAndValue.Append( aValue );
       
   563 
       
   564 	TPtrC pBuf = iReadBuf.Mid( iReadBufPosition );
       
   565 
       
   566 	TInt err;
       
   567 	TInt tagStartPosition;
       
   568 		
       
   569 	err = pBuf.Find( tagAndValue );
       
   570 	if (err < 0)
       
   571 		{
       
   572 		LOGGER_WRITE( "Message tag with value not found" );
       
   573 		return err;
       
   574 		}
       
   575 	else 
       
   576 		{
       
   577 		tagStartPosition = iReadBufPosition + err; // position before tag and value
       
   578 		iReadBufPosition += ( err + tagAndValue.Length() ); // position after tag and value
       
   579 		}
       
   580 
       
   581 	return tagStartPosition; 
       
   582 	}
       
   583 
       
   584 // -----------------------------------------------------------------------------
       
   585 // CVMessageParser::GetMessageTagValue()
       
   586 // -----------------------------------------------------------------------------
       
   587 //
       
   588 TInt CVMessageParser::GetMessageTagValue( const TDesC& aMsgTag, TDes& aValue, TBool aMoveReadBufPosition )
       
   589 	{
       
   590 	TInt err;
       
   591 	TInt valueLength;
       
   592 	TInt readBufPosition( iReadBufPosition );
       
   593 
       
   594 	TPtrC pBuf = iReadBuf.Mid( readBufPosition );
       
   595 	
       
   596 	err = pBuf.Find( aMsgTag );
       
   597 	if (err < 0)
       
   598 		{	
       
   599 		LOGGER_WRITE( "Message tag not found" );
       
   600 		return err; 
       
   601 		}
       
   602 	else
       
   603 		{
       
   604 		readBufPosition += (err + aMsgTag.Length());
       
   605 		}
       
   606 
       
   607 	TPtrC pValueStart = iReadBuf.Mid( readBufPosition ); 
       
   608 
       
   609 	err = pValueStart.Find( KVMsgLineFeed );
       
   610 	if (err < 0)
       
   611 		{
       
   612 		return err; 
       
   613 		}
       
   614 	else
       
   615 		{
       
   616 		valueLength = err;
       
   617 		}
       
   618 
       
   619 	StoreMessageDataField( readBufPosition, valueLength, aValue );
       
   620 	
       
   621 	if ( aMoveReadBufPosition )
       
   622 		{
       
   623 		iReadBufPosition = readBufPosition;
       
   624 		}
       
   625 
       
   626 	return valueLength;
       
   627 	}
       
   628 	
       
   629 // -----------------------------------------------------------------------------
       
   630 // CVMessageParser::StoreMessageDataField()
       
   631 // -----------------------------------------------------------------------------
       
   632 //
       
   633 void CVMessageParser::StoreMessageDataField(TInt aStart, TInt aLength, TDes &aLocalStore)
       
   634 	{
       
   635 	TInt copyLength;
       
   636 
       
   637 	copyLength = aLength > aLocalStore.MaxLength() ? aLocalStore.MaxLength() : aLength;
       
   638 
       
   639 	TPtrC pData = iReadBuf.Mid( aStart, copyLength );
       
   640 	aLocalStore.Copy( pData );
       
   641 	aLocalStore.Trim();
       
   642 
       
   643 	if (copyLength < aLength)
       
   644 		{
       
   645 		LOGGER_WRITE_2( "Data length: %d, stored: %d", aLength, copyLength );	
       
   646 		}
       
   647 	}
       
   648 
       
   649 // -----------------------------------------------------------------------------
       
   650 // CVMessageParser::WriteMessagePropertiesL()
       
   651 // -----------------------------------------------------------------------------
       
   652 //
       
   653 void CVMessageParser::WriteMessagePropertiesL()
       
   654 	{
       
   655 	LOGGER_WRITE( "CVMessageParser::WriteMessagePropertiesL()" );
       
   656 	WriteMessageLineL( KVMsgTagVersion, KVMsgVersion );
       
   657 	WriteMessageLineL( KVMsgTagStatus, iStatus );
       
   658 	WriteMessageLineL( KVMsgTagBox, iFolder );
       
   659 	WriteTaggedTimestampL( iUniversalTime );
       
   660 	}
       
   661 
       
   662 // -----------------------------------------------------------------------------
       
   663 // CVMessageParser::WriteMessageVCARDL()
       
   664 // -----------------------------------------------------------------------------
       
   665 //
       
   666 void CVMessageParser::WriteMessageVCARDL(const TDesC& aName, const TDesC& aNumber)
       
   667 	{
       
   668 	LOGGER_WRITE( "CVMessageParser::WriteMessageVCARDL()" );
       
   669 	WriteMessageLineL( KVMsgTagBegin, KVMsgSectionVCard );
       
   670 	WriteMessageLineL( KVMsgTagVersion, KVMsgVCardVersion );
       
   671 	WriteMessageLineL( KVMsgTagName, aName );
       
   672 	WriteMessageLineL( KVMsgTagTelephone, aNumber );
       
   673 	WriteMessageLineL( KVMsgTagEnd, KVMsgSectionVCard );
       
   674 	}
       
   675 
       
   676 // -----------------------------------------------------------------------------
       
   677 // CVMessageParser::WriteMessageEnvelopeL()
       
   678 // -----------------------------------------------------------------------------
       
   679 //
       
   680 void CVMessageParser::WriteMessageEnvelopeL()
       
   681 	{
       
   682 	LOGGER_WRITE( "CVMessageParser::WriteMessageEnvelopeL()" );
       
   683 
       
   684 	TInt i;
       
   685 	TInt numRecipients = iRecipients.Count();
       
   686 
       
   687 	for (i = 0; i < numRecipients; i++)
       
   688 		{
       
   689 		WriteMessageLineL( KVMsgTagBegin, KVMsgSectionVEnv );
       
   690 		WriteMessageVCARDL( iRecipients[i].iName, iRecipients[i].iNumber );		
       
   691 		}
       
   692 
       
   693 	WriteMessageLineL( KVMsgTagBegin, KVMsgSectionVEnv );
       
   694 	WriteMessageBodyL();
       
   695 	WriteMessageLineL( KVMsgTagEnd, KVMsgSectionVEnv );
       
   696 
       
   697 	for (i = 0; i < numRecipients; i++)
       
   698 		{
       
   699 		WriteMessageLineL( KVMsgTagEnd, KVMsgSectionVEnv );	
       
   700 		}	
       
   701 	}
       
   702 
       
   703 // -----------------------------------------------------------------------------
       
   704 // CVMessageParser::WriteTaggedTimestampL()
       
   705 // -----------------------------------------------------------------------------
       
   706 //	
       
   707 void CVMessageParser::WriteTaggedTimestampL( TTime aUniversalTime )
       
   708 	{
       
   709 	LOGGER_WRITE( "CVMessageParser::WriteTaggedTimestamp()" );
       
   710 	
       
   711 	TDateTime dateTime = aUniversalTime.DateTime();
       
   712 	
       
   713 	TBuf<16> timestamp;
       
   714 	
       
   715 	// Default timesamp format: yyyymmddThhmmssZ - like 20060329T091530Z	
       
   716 	_LIT( KTimestampFormat, "%+04d%+02d%+02dT%+02d%+02d%+02dZ" );
       
   717 	timestamp.Format( KTimestampFormat, 
       
   718 		dateTime.Year(),
       
   719 		dateTime.Month() + 1,
       
   720 		dateTime.Day() + 1,
       
   721 		dateTime.Hour(),
       
   722 		dateTime.Minute(),
       
   723 		dateTime.Second() );
       
   724 		
       
   725 	WriteMessageLineL( KVMsgTagDateTime, timestamp );	
       
   726 	}
       
   727 
       
   728 // -----------------------------------------------------------------------------
       
   729 // CVMessageParser::WriteMessageBodyL()
       
   730 // -----------------------------------------------------------------------------
       
   731 //
       
   732 void CVMessageParser::WriteMessageBodyL()
       
   733 	{
       
   734 	LOGGER_WRITE( "CVMessageParser::WriteMessageBodyL()" );
       
   735 
       
   736 	WriteMessageLineL( KVMsgTagBegin, KVMsgSectionVBody );
       
   737 
       
   738     // "Date" field is always device local time, not UTC
       
   739 	TBuf<KDateMaxLength> dateStr;
       
   740 	_LIT( KDateFormat, "%F%*D.%*M.%Y% %H:%T:%S" );
       
   741 	iHomeTime.FormatL( dateStr, KDateFormat );
       
   742 	WriteMessageLineL(KVMsgTagDate, dateStr );
       
   743 
       
   744 	if (iMessageBody)
       
   745 		{
       
   746 		LOG(*iMessageBody);
       
   747 		WriteMessageLineL( KDesNoData, *iMessageBody, EFalse );
       
   748 		}
       
   749 	else
       
   750 		{
       
   751 		LOGGER_WRITE( "Message body empty" );
       
   752 		}
       
   753 
       
   754 	WriteMessageLineL( KVMsgTagEnd, KVMsgSectionVBody );
       
   755 	}
       
   756 
       
   757 // -----------------------------------------------------------------------------
       
   758 // CVMessageParser::WriteMessageLineL()
       
   759 // -----------------------------------------------------------------------------
       
   760 //
       
   761 void CVMessageParser::WriteMessageLineL( const TDesC& aMsgLineTag, const TDesC& aMsgLineData, TBool aIndent )
       
   762 	{
       
   763 	if (aIndent)
       
   764 		{
       
   765 		TInt indent = GetIndentLevel( aMsgLineTag );
       
   766 		TBuf<KTagAndValueMaxLength> indentSpace;
       
   767 		indentSpace.AppendFill( TChar(' '), indent );
       
   768 		WriteToMessageL( indentSpace );
       
   769 		}
       
   770 
       
   771 	if (aMsgLineTag.Length() > 0)
       
   772 		{
       
   773 		WriteToMessageL( aMsgLineTag );
       
   774 		}
       
   775 
       
   776 	WriteToMessageL( aMsgLineData );	
       
   777 	WriteToMessageL( KVMsgLineFeed );
       
   778 	}
       
   779 
       
   780 // -----------------------------------------------------------------------------
       
   781 // CVMessageParser::WriteToMessageL()
       
   782 // -----------------------------------------------------------------------------
       
   783 //
       
   784 void CVMessageParser::WriteToMessageL( const TDesC &aData )
       
   785 	{
       
   786 	TPtrC8 writeData;
       
   787 
       
   788 	const TUint16* ptr16 = aData.Ptr();
       
   789 	const TUint8* ptr8 = reinterpret_cast<const TUint8*>(ptr16);
       
   790 	writeData.Set( ptr8, aData.Length()*2 );
       
   791 
       
   792 	TInt newPosition = iWriteBufPosition + writeData.Length();
       
   793 
       
   794 	if (newPosition > iWriteBufSize)
       
   795 		{
       
   796 		LOGGER_WRITE( "Expand write buffer" );
       
   797 		TInt expandStep = newPosition - iWriteBufSize + 100;
       
   798 		iWriteBuf->ExpandL( iWriteBufSize, expandStep );
       
   799 		iWriteBufSize += expandStep;
       
   800 		LOGGER_WRITE_1( "Write buffer expanded to %d bytes", iWriteBufSize );
       
   801 		}
       
   802 
       
   803 	iWriteBuf->Write( iWriteBufPosition, writeData );
       
   804 	iWriteBufPosition = newPosition;
       
   805 	}
       
   806 
       
   807 
       
   808 // -----------------------------------------------------------------------------
       
   809 // CVMessageParser::GetIndentLevel()
       
   810 // -----------------------------------------------------------------------------
       
   811 //
       
   812 TInt CVMessageParser::GetIndentLevel( const TDesC& aMsgLineTag )
       
   813 	{
       
   814 	if (aMsgLineTag.Compare( KVMsgTagEnd ) == 0)
       
   815 		{
       
   816 		iWriteIndentLevel--;
       
   817 		}
       
   818 	TInt result = iWriteIndentLevel * KIndentStepSize;
       
   819 
       
   820 	if (aMsgLineTag.Compare( KVMsgTagBegin ) == 0)
       
   821 		{
       
   822 		iWriteIndentLevel++;
       
   823 		}
       
   824 	return result;
       
   825 	}
       
   826 
       
   827 // -----------------------------------------------------------------------------
       
   828 // CVMessageParser::AssignDataToBuffer()
       
   829 // -----------------------------------------------------------------------------
       
   830 //
       
   831 void CVMessageParser::AssignDataToBuffer( const TDesC &aData, TDes &aBuffer )
       
   832 	{
       
   833 	aBuffer.Zero();
       
   834 
       
   835 	if (aBuffer.MaxLength() >= aData.Length())
       
   836 		{
       
   837 		aBuffer = aData;
       
   838 		}
       
   839 	else
       
   840 		{
       
   841 		LOGGER_WRITE_1( "AssignDataToBuffer: too short buffer for data: %d", aBuffer.MaxLength() );
       
   842 		LOG( aData );
       
   843 		aBuffer = aData.Left( aBuffer.MaxLength() );
       
   844 		}
       
   845 	}
       
   846 
       
   847 
       
   848 //  End of File