messagingfw/biomsgfw/BIUTSRC/BSP.CPP
changeset 0 8e480a14352b
child 44 7c176670643f
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1998-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 // BSP.CPP (Base Script Parser)
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "bsp.h"
       
    19 #include "regpsdll.h"	// CRegisteredParserDll
       
    20 #include "msventry.h"	// CMsvServerEntry
       
    21 #include <msvuids.h>	// KUidMsvMessageEntry, KUidMsvServiceEntry
       
    22 #include <msvids.h>
       
    23 #include <biouids.h>
       
    24 #include <biodb.h>
       
    25 
       
    26 const TInt KMaxStringLength = 1024;
       
    27 
       
    28 EXPORT_C CBaseScriptParser::CBaseScriptParser(CRegisteredParserDll& aRegisteredParserDll,CMsvServerEntry& aEntry, RFs& aFs)
       
    29 :CActive(EPriorityStandard), iRegisteredParserDll(aRegisteredParserDll), iEntry(aEntry), iFs(aFs)
       
    30 /** Constructor.
       
    31 
       
    32 This is called by CBIOServerMtm to create a parser object.
       
    33 
       
    34 @param aRegisteredParserDll Object that loaded the parser. It contains a reference 
       
    35 counter of the use of the parser.
       
    36 @param aEntry The message entry the parser should parse
       
    37 @param aFs Connected file server handle */
       
    38 	{
       
    39 	iEntryId = iEntry.Entry().Id(); //id of SMS entry
       
    40 	}
       
    41 
       
    42 EXPORT_C CBaseScriptParser::~CBaseScriptParser()
       
    43 /** Destructor.
       
    44 
       
    45 This deletes iSettings and iReadBuffer and calls iRegisteredParserDll.ReleaseLibrary(). */
       
    46 	{
       
    47 	delete iReadBuffer;
       
    48 	delete iSettings;
       
    49 	iRegisteredParserDll.ReleaseLibrary();
       
    50 	}
       
    51 
       
    52 
       
    53 EXPORT_C void CBaseScriptParser::UnfoldMessageL()
       
    54 /** Utility function for unfolding Smart Messages.
       
    55 
       
    56 Nokia protocol allows for folding of long fields (see Nokia Smart Messaging 
       
    57 spec 2.0.0pre, 3-34 and RFC822, 3.1.1). This method unfolds the message by 
       
    58 deleting any linefeed characters which are followed immediately by linear 
       
    59 white space. It expects the buffer to be in iSmsBuf. */
       
    60     {
       
    61     // Nokia protocol allows for folding of long fields (see Nokia Smart
       
    62     // Messaging spec 2.0.0pre, 3-34 and RFC822, 3.1.1). This method
       
    63     // unfolds the message by deleting any linefeed characters which are
       
    64     // followed immediately by linear white space.
       
    65     //   Note that the value returned by pBuf.Length() will change if
       
    66     // linefeeds are deleted. Hence this is called for each iteration to
       
    67     // avoid violating buffer bounds.
       
    68 
       
    69     TPtr pBuf(iSmsBuf->Des());				// Create modifiable pointer to HBufC
       
    70 
       
    71     for (TInt pos = 0; pos < (pBuf.Length() - 1); pos++)
       
    72         {
       
    73         // Find linefeed followed by whitespace
       
    74         if (pBuf[pos] == KCharLineFeed  &&
       
    75             (pBuf[pos+1] == KCharSpace  ||  pBuf[pos+1] == KCharTab))
       
    76             {
       
    77             pBuf.Delete(pos, 1);
       
    78             }
       
    79         }
       
    80     iSmsBuf = iSmsBuf->ReAllocL(pBuf.Length());		// Reallocate iSmsBuf with new size.
       
    81     }
       
    82 // end CBaseScriptParser::UnfoldSmsL()
       
    83 
       
    84 
       
    85 void CBaseScriptParser::InternalizeL(RMsvReadStream& aReadStream)
       
    86 	{
       
    87 	ResetL();
       
    88 	iParsedFieldArray = new(ELeave) CArrayPtrSeg<CParsedField>(16);
       
    89 
       
    90 	CParsedField* parsedField = NULL;
       
    91 	TInt count = aReadStream.ReadUint8L();
       
    92 	for (TInt i=0; i < count; i++)
       
    93 		{
       
    94 		parsedField = new (ELeave) CParsedField();
       
    95 		CleanupStack::PushL(parsedField);
       
    96 		parsedField->InternalizeL(aReadStream);
       
    97 		iParsedFieldArray->AppendL(parsedField);
       
    98 		CleanupStack::Pop(parsedField); 
       
    99 		}
       
   100 	}
       
   101 
       
   102 void CBaseScriptParser::ExternalizeL(RMsvWriteStream& aStream) const
       
   103 	{
       
   104 	TInt count = iParsedFieldArray->Count();
       
   105 	aStream.WriteInt8L(count);
       
   106 	for(TInt number = 0; number<count; number++)	// must keep order, go forwards
       
   107 		{
       
   108 		aStream << *(*iParsedFieldArray)[number];
       
   109 		}
       
   110 	}
       
   111 
       
   112 EXPORT_C void CBaseScriptParser::StoreL(CMsvStore& aMsvStore) const
       
   113 /** Stores the parsed fields array.
       
   114 
       
   115 It stores the iParsedFieldArray array in the specified CMsvStore.
       
   116 
       
   117 @param aMsvStore Store to write to */
       
   118 	{
       
   119 	RMsvWriteStream out;
       
   120 	out.AssignLC( aMsvStore, KUidMsvBIODataStream); // pushes 'out' to the stack
       
   121 	TRAPD(error, ExternalizeL(out));
       
   122 	if (error==KErrNone)
       
   123 		out.CommitL();
       
   124 	out.Close(); // make sure we close the file
       
   125 	User::LeaveIfError(error);
       
   126 	aMsvStore.CommitL();
       
   127 	CleanupStack::PopAndDestroy();
       
   128 	}
       
   129 
       
   130 EXPORT_C void CBaseScriptParser::RestoreL( CMsvStore& aMessageStore )
       
   131 /** Restores the parsed fields array.
       
   132 
       
   133 It restores the iParsedFieldArray array from the specified CMsvStore.
       
   134 
       
   135 @param aMessageStore Store to read from */
       
   136 	{
       
   137 	RMsvReadStream in;
       
   138 	in.OpenLC( aMessageStore, KUidMsvBIODataStream );
       
   139 	InternalizeL(in);
       
   140 	CleanupStack::PopAndDestroy();
       
   141 	}
       
   142 
       
   143 //
       
   144 //	write contents of Smart message to a file as attachment
       
   145 //
       
   146 EXPORT_C void CBaseScriptParser::StoreL(const TFileName& aFileName) const
       
   147 /** Stores the message data in a specified file.
       
   148 
       
   149 It stores iSettings in the specified file.
       
   150 
       
   151 @param aFileName File to write to */
       
   152 	{
       
   153 	//
       
   154 	TFileName filePath;
       
   155 	// creates directory for current entry to put file into
       
   156 	iEntry.GetFilePath(filePath);
       
   157 
       
   158 	// create the full file name with path
       
   159 	TParse parse;
       
   160 	parse.Set(aFileName, &filePath, NULL);
       
   161 	// create the file
       
   162 	RFile file;
       
   163 	User::LeaveIfError(file.Replace(iFs, parse.FullName(), EFileWrite|EFileShareExclusive|EFileStream));
       
   164 	CleanupClosePushL(file);
       
   165 	
       
   166 
       
   167 	// if it worked stream data to it
       
   168 	// first copy data into 8-bit buffer (it's 8bit content anyway?)
       
   169 	TPtr dataContents = iSettings->Des();
       
   170 	HBufC8* buf = HBufC8::NewLC(dataContents.Length());
       
   171 	buf->Des().Copy(dataContents);
       
   172 
       
   173 	TInt fileErr = file.Write( buf->Des() );
       
   174 	file.Close();
       
   175 	if(fileErr!=KErrNone)
       
   176 		{
       
   177 		iFs.Delete(parse.FullName());
       
   178 		User::Leave(fileErr);
       
   179 		}
       
   180 	
       
   181 	CleanupStack::PopAndDestroy(2); // buf, file
       
   182 	}
       
   183 
       
   184 
       
   185 EXPORT_C void CBaseScriptParser::RestoreL( const TFileName& aFileName )
       
   186 /** Restores the message data from a specified file.
       
   187 
       
   188 It restores iSettings from the specified file.
       
   189 
       
   190 @param aFileName File to read from */
       
   191 	{
       
   192 	//
       
   193 	TFileName filePath;
       
   194 	// creates directory for current entry to put file into
       
   195 	iEntry.GetFilePath(filePath);
       
   196 
       
   197 	// create the full file name with path
       
   198 	TParse parse;
       
   199 	parse.Set(aFileName, &filePath, NULL);
       
   200 	// create the file
       
   201 	RFile file;
       
   202 	User::LeaveIfError(file.Open(iFs, parse.FullName(), EFileShareExclusive|EFileStream));
       
   203 	CleanupClosePushL(file);
       
   204 
       
   205 	// read into descriptor resizing as we go
       
   206 	iReadBuffer = HBufC8::NewL(256);
       
   207 	TBuf8<256> fileChunk;
       
   208 	TInt fileErr = KErrNone;
       
   209 	while(fileErr==KErrNone)
       
   210 		{
       
   211 		fileErr = file.Read(fileChunk, fileChunk.MaxLength());
       
   212 		if(fileChunk.Length()==0)
       
   213 			break;
       
   214 		// check if we need to resize
       
   215 		TInt newLength = iReadBuffer->Des().Length() + fileChunk.Length();
       
   216 		if( newLength > iReadBuffer->Des().MaxLength())
       
   217 			{
       
   218 			iReadBuffer = iReadBuffer->ReAllocL(newLength);
       
   219 			}
       
   220 		iReadBuffer->Des().Append(fileChunk);
       
   221 		}
       
   222 	// if it worked stream data to it
       
   223 	// first copy data into 8-bit buffer (it's 8bit content anyway?)
       
   224 	file.Close();
       
   225 	if(fileErr!=KErrNone)
       
   226 		User::Leave(fileErr);
       
   227 	CleanupStack::PopAndDestroy(1); // file
       
   228 	delete iSettings;
       
   229 	iSettings = NULL;
       
   230 	iSettings = HBufC::NewL(iReadBuffer->Length());
       
   231 	iSettings->Des().Copy(iReadBuffer->Des());
       
   232 	} 
       
   233 
       
   234 EXPORT_C TUid CBaseScriptParser::ParserUid()
       
   235 /** Gets the UID of the BIO message type handled by the parser.
       
   236 
       
   237 @return BIO message type UID */
       
   238 	{
       
   239 	return (iRegisteredParserDll.UidType()[2]);
       
   240 	}
       
   241 
       
   242 EXPORT_C void CBaseScriptParser::ResetL()
       
   243 /** Deletes the iParsedFieldArray parsed fields array and sets it to NULL. */
       
   244 	{
       
   245 	// delete previous array and create a new one
       
   246 	if(iParsedFieldArray)
       
   247 		{
       
   248 		iParsedFieldArray->ResetAndDestroy();
       
   249 		delete iParsedFieldArray;
       
   250 		iParsedFieldArray=NULL;
       
   251 		}
       
   252 	}
       
   253 
       
   254 //
       
   255 // Class CParsed field container class for 
       
   256 // data item extracted from smart message
       
   257 //
       
   258 EXPORT_C CParsedField::CParsedField()
       
   259 /** Constructor. */
       
   260 	{
       
   261 	iFieldName =NULL;
       
   262 	iFieldValue =NULL;
       
   263 	iMandatoryField =ETrue;
       
   264 	}
       
   265 
       
   266 EXPORT_C CParsedField::~CParsedField()
       
   267 /** Destructor. */
       
   268 	{
       
   269 	delete iFieldName;
       
   270 	delete iFieldValue;
       
   271 	}
       
   272 
       
   273 
       
   274 EXPORT_C TPtrC CParsedField::FieldName() const
       
   275 /** Gets the field name.
       
   276 
       
   277 @return Field name */
       
   278 	{
       
   279 	return iFieldName->Des();
       
   280 	}
       
   281 
       
   282 
       
   283 EXPORT_C void CParsedField::SetFieldNameL(const TDesC& aFieldName)
       
   284 /** Sets the field name.
       
   285 
       
   286 @param aFieldName Field name */
       
   287 	{
       
   288 	HBufC* temp =aFieldName.AllocL();
       
   289 	delete iFieldName;
       
   290 	iFieldName = temp;	
       
   291 	}
       
   292 
       
   293 
       
   294 EXPORT_C TPtrC CParsedField::FieldValue() const 
       
   295 /** Gets the field value.
       
   296 
       
   297 @return Field value */
       
   298 	{
       
   299 	return iFieldValue->Des();
       
   300 	}
       
   301 
       
   302 
       
   303 EXPORT_C void CParsedField::SetFieldValueL(const TDesC& aFieldValue)
       
   304 /** Sets the field value.
       
   305 
       
   306 @param aFieldValue Field value */
       
   307 	{
       
   308 	HBufC* temp =aFieldValue.AllocL();
       
   309 	delete iFieldValue;
       
   310 	iFieldValue = temp;
       
   311 	}
       
   312 
       
   313 
       
   314 EXPORT_C TBool CParsedField::MandatoryField() const 
       
   315 /** Tests if this is a mandatory field.
       
   316 
       
   317 @return True if this is a mandatory field */
       
   318 	{
       
   319 	return iMandatoryField;
       
   320 	}
       
   321 
       
   322 
       
   323 EXPORT_C void CParsedField::SetMandatoryField(TBool aMandatoryField)
       
   324 /** Sets/unsets this as a mandatory field.
       
   325 
       
   326 @param aMandatoryField True if this is a mandatory field, false if not */
       
   327 	{
       
   328 	iMandatoryField = aMandatoryField;
       
   329 	}
       
   330 
       
   331 EXPORT_C void CParsedField::InternalizeL(RReadStream& aStream)
       
   332 /** Internalises the object.
       
   333 
       
   334 @param aStream Stream to read from */
       
   335 	{
       
   336 	Reset();
       
   337 	delete iFieldName;
       
   338 	iFieldName = NULL;
       
   339 	iFieldName = HBufC::NewL( aStream, KMaxStringLength );
       
   340 	TInt fieldValueLength = aStream.ReadInt16L();
       
   341 	delete iFieldValue;
       
   342 	iFieldValue = NULL;
       
   343 	iFieldValue = HBufC::NewL( aStream, fieldValueLength );
       
   344 	iMandatoryField = aStream.ReadInt8L();
       
   345 	}
       
   346 
       
   347 EXPORT_C void CParsedField::ExternalizeL(RWriteStream& aStream) const
       
   348 /** Externalises the object.
       
   349 
       
   350 @param aStream Stream to write to */
       
   351 	{
       
   352 	aStream << *iFieldName;
       
   353 	aStream.WriteUint16L( iFieldValue->Length() );
       
   354 	aStream << *iFieldValue;
       
   355 	aStream.WriteUint8L( iMandatoryField ? 1 : 0);
       
   356 	}
       
   357 
       
   358 //
       
   359 // Reset contetnts of field
       
   360 //
       
   361 void CParsedField::Reset()
       
   362 	{
       
   363 	delete iFieldName;
       
   364 	iFieldName = NULL;
       
   365 
       
   366 	delete iFieldValue;
       
   367 	iFieldValue = NULL;
       
   368 
       
   369 	iMandatoryField= EFalse;
       
   370 	}
       
   371