email/pop3andsmtpmtm/clientmtms/src/MIUTATCH.CPP
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     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 //
       
    15 
       
    16 #if !defined (__E32STD_H__)
       
    17 #include <e32std.h>
       
    18 #endif
       
    19 
       
    20 #if !defined (__MIUTATCH_H__)
       
    21 #include "MIUTATCH.H"
       
    22 #endif
       
    23 
       
    24 void TImAttachmentFile::Reset()
       
    25 	{
       
    26 	iWritingToFile = EFalse;
       
    27 	iSpaceOnCache = 0;
       
    28 	iPositionInCache = 0;
       
    29 	iDataLine.SetLength(0);
       
    30 	}
       
    31 
       
    32 EXPORT_C TImAttachmentFile::TImAttachmentFile(RFs& aFileSession)
       
    33 				: iFileSession(aFileSession)
       
    34 	{
       
    35 	Reset();
       
    36 	}
       
    37 EXPORT_C TImAttachmentFile::~TImAttachmentFile()
       
    38 	{
       
    39 	iFile.Close();
       
    40 	}
       
    41 
       
    42 EXPORT_C TInt TImAttachmentFile::MakeDir (const TDesC& aDirPath)
       
    43 	{
       
    44 	// Create that directory
       
    45 	// Nb directory name must be terminated with '\\' character
       
    46 	return (iFileSession.MkDir(aDirPath));
       
    47 	}
       
    48 
       
    49 EXPORT_C TInt TImAttachmentFile::OpenFile (const TDesC& aDirPath, const TDesC& aFileName)
       
    50 
       
    51 	{
       
    52 	TInt err(AppendValidFile(aDirPath, aFileName, iFullFilePath));
       
    53 	if (err!=KErrNone)
       
    54 		return err;
       
    55 	Reset();
       
    56 	iWritingToFile = EFalse;
       
    57 	return (iFile.Open(iFileSession, iFullFilePath, EFileShareExclusive|EFileStream));
       
    58 	}
       
    59 
       
    60 EXPORT_C TInt TImAttachmentFile::CreateFile (const TDesC& aDirPath, const TDesC& aFileName)
       
    61 	{
       
    62 	// Initialise buffer used to store attachment data, prior to writing to file
       
    63 	iDataLine.FillZ();
       
    64 	Reset();
       
    65 	
       
    66 	// Create a file for the attachment data
       
    67 	// If the file already exists in this path, generate a new file name
       
    68 	// with the path: "\aDirPath\aFileName(i)" where "i">0 
       
    69 
       
    70 	TInt error=MakeDir(aDirPath);	// make directory for the attachment
       
    71 	if (error!=KErrAlreadyExists && error!=KErrNone)
       
    72 		return error;
       
    73 	
       
    74 	error=AppendValidFile(aDirPath, aFileName, iFullFilePath);
       
    75 	if (error!=KErrNone)
       
    76 		return error;
       
    77 
       
    78 	error=iFile.Create(iFileSession, iFullFilePath, EFileWrite|EFileShareExclusive|EFileStream);
       
    79 
       
    80 	if (error==KErrNone)
       
    81 		{
       
    82 		iWritingToFile = ETrue;
       
    83 		return error;
       
    84 		}
       
    85 	if (error!=KErrAlreadyExists)
       
    86 		return error;
       
    87 			
       
    88 	TParsePtr filenamePPtr(iFullFilePath);
       
    89 	TFileName newFileName;
       
    90 
       
    91 	TInt index=0;
       
    92 	do 
       
    93 		{
       
    94 		TPtrC filename=filenamePPtr.Name();
       
    95 		TPtrC extension=filenamePPtr.Ext();
       
    96 		iFullFilePath.Format(KImcvAttachFormatStr,&filename,++index,&extension);
       
    97 		error=AppendValidFile(aDirPath, iFullFilePath, newFileName);
       
    98 		if (error!=KErrNone)
       
    99 			error=iFile.Create(iFileSession, newFileName, EFileWrite|EFileShareExclusive|EFileStream);
       
   100 		}
       
   101 	while (error==KErrAlreadyExists);
       
   102 	return error;
       
   103 	}
       
   104 
       
   105 EXPORT_C TInt TImAttachmentFile::ReadFile (TDes8& rBuffer,TInt aLength)
       
   106 	{
       
   107 	// Only read from file once in a while, put into an intermediate buffer.
       
   108 	// Pass out data from buffer. Copy aLength of data from iDataLine to rBuffer
       
   109 
       
   110 	TInt err = 0;			// Error returned by file read.
       
   111 	TInt bufLength = aLength;	// Length yet to be copied.  
       
   112 
       
   113 	// Is there anything in the buffer?
       
   114 	TInt dataLineLength = iDataLine.Length();
       
   115 	if (dataLineLength==0)
       
   116 		{
       
   117 		// Is length<buffer size?
       
   118 		if (bufLength<KDataLineBufferLength)
       
   119 			{
       
   120 			// Fill buffer
       
   121 			err = iFile.Read(iDataLine, KDataLineBufferLength);
       
   122 			dataLineLength = iDataLine.Length();
       
   123 
       
   124 			// Inititialise
       
   125 			iPositionInCache = 0;
       
   126 			iSpaceOnCache = dataLineLength;
       
   127 
       
   128 			// Read 'length' bytes from the data
       
   129 			rBuffer = SelectFileData(bufLength, iSpaceOnCache);
       
   130 			bufLength = 0;
       
   131 			}
       
   132 		else
       
   133 			{
       
   134 			// Don't use intermediate buffer
       
   135 			// Read 'length' bytes from file to rBuffer
       
   136 			err = iFile.Read(rBuffer,bufLength);
       
   137 			bufLength = 0;
       
   138 			}
       
   139 		}
       
   140 	else
       
   141 		// Is 'length'<data available in buffer
       
   142 		if (bufLength<iSpaceOnCache)
       
   143 			{
       
   144 			rBuffer = SelectFileData(bufLength, dataLineLength);
       
   145 			bufLength = 0;
       
   146 			}
       
   147 		else
       
   148 			{
       
   149 			// Read length bytes from the buffer to rBuffer
       
   150 			rBuffer = SelectFileData(iSpaceOnCache, dataLineLength);
       
   151 			bufLength -= rBuffer.Length();
       
   152 
       
   153 			if (bufLength>0)
       
   154 				{
       
   155 				if (bufLength<KDataLineBufferLength)
       
   156 					{
       
   157 					// Fill buffer
       
   158 					err = iFile.Read(iDataLine, KDataLineBufferLength);
       
   159 					dataLineLength = iDataLine.Length();
       
   160 
       
   161 					// Inititialise
       
   162 					iPositionInCache = 0;
       
   163 					iSpaceOnCache = dataLineLength;
       
   164 
       
   165 					// Read 'length' bytes from the data
       
   166 					rBuffer.Append(SelectFileData(bufLength, dataLineLength));
       
   167 					}
       
   168 				else
       
   169 					{
       
   170 					// read in remaining data to the rBuffer
       
   171 					TInt bufiPositionInCache = aLength-bufLength;
       
   172 					rBuffer.SetLength(bufiPositionInCache+1); // need this because otherwise get a panic!
       
   173 					TPtr8 ptr(&rBuffer[bufiPositionInCache], bufLength);
       
   174 					err = iFile.Read(ptr);
       
   175 					TInt ptrLength = ptr.Length();
       
   176 					rBuffer.SetLength(bufiPositionInCache+ptrLength);	// set the original length
       
   177 					bufLength = 0;
       
   178 					}
       
   179 				}
       
   180 			}
       
   181 	return err;
       
   182 	}
       
   183 
       
   184 
       
   185 TPtrC8 TImAttachmentFile::SelectFileData(TInt& aBufLen, TInt& aDataLen)
       
   186 	{
       
   187 	// Read length bytes from the buffer to rBuffer
       
   188 	TInt size = aBufLen<aDataLen ? aBufLen : aDataLen;
       
   189 
       
   190 	TPtrC8 rBuf = iDataLine.Mid(iPositionInCache,size);
       
   191 	iPositionInCache += size;
       
   192 	iSpaceOnCache -= size;
       
   193 	return rBuf;
       
   194 	}
       
   195 
       
   196 
       
   197 TInt TImAttachmentFile::ReadFile ()
       
   198 	{
       
   199 	return iFile.Read(iDataLine);
       
   200 	}
       
   201 	
       
   202 EXPORT_C void TImAttachmentFile::SetFileHandle(RFile& aFile,TImFileOpenMode aFileMode)	
       
   203 	{
       
   204 	if(aFileMode == EImFileRead)
       
   205 		iWritingToFile = EFalse;
       
   206 	else if(aFileMode == EImFileWrite)
       
   207 		iWritingToFile = ETrue;
       
   208 	
       
   209 	iFile = aFile;
       
   210 	}
       
   211 
       
   212 EXPORT_C TInt TImAttachmentFile::WriteFile (const TDesC8& aBuffer)
       
   213 	{
       
   214 	// Write data from aBuffer to file using an intermediate buffer, iDataLine. 
       
   215 	// This is to reduce the number of file writes. 'Large' files can be written 
       
   216 	// directly to the file.
       
   217 	
       
   218 	TInt err(KErrNone);
       
   219 
       
   220 	TInt atchDataSize = aBuffer.Length();
       
   221 	TInt bufferSize = iDataLine.Length();
       
   222 	TInt spaceOnBuffer = 0;
       
   223 	TInt bufPos = 0;
       
   224 	// Fill buffer
       
   225 	spaceOnBuffer = KDataLineBufferLength - bufferSize;
       
   226 
       
   227 	// if incoming data is bigger than buffer 
       
   228 	// we can write it directly to file if packing is not done earier
       
   229 	if (atchDataSize >= KDataLineBufferLength && spaceOnBuffer == KDataLineBufferLength)
       
   230 		{
       
   231 		err=iFile.Write(aBuffer.Mid(bufPos));
       
   232 		atchDataSize=0;
       
   233 		return err;
       
   234 		}
       
   235 
       
   236 	if (atchDataSize > spaceOnBuffer)
       
   237 		{
       
   238 		// The remaining size of the atch is greater than the buffer
       
   239 		// Append as much data from the atch than will fit in the buffer
       
   240 		iDataLine.Append(aBuffer.Mid(bufPos,spaceOnBuffer));
       
   241 
       
   242 		bufPos += spaceOnBuffer;
       
   243 		atchDataSize -= spaceOnBuffer;
       
   244 
       
   245 		// Flush buffer to file
       
   246 		err = WriteFile();
       
   247 		iDataLine.SetLength(0);
       
   248 		bufferSize = 0;
       
   249 		
       
   250 		// if the size of the attachment is 'large' then write the whole file
       
   251 		if (atchDataSize >= KDataLineBufferLength)
       
   252 			{
       
   253 			err=iFile.Write(aBuffer.Mid(bufPos));
       
   254 			atchDataSize=0;
       
   255 			}
       
   256 		else 
       
   257 			{
       
   258 			iDataLine.Append(aBuffer.Mid(bufPos,atchDataSize));
       
   259 			bufPos += atchDataSize;
       
   260 			atchDataSize -= atchDataSize;	// atchDataSize should be 0!
       
   261 			}
       
   262 		}
       
   263 	else 
       
   264 		{
       
   265 		iDataLine.Append(aBuffer.Mid(bufPos,atchDataSize));
       
   266 		bufPos += atchDataSize;
       
   267 		atchDataSize -= atchDataSize;	// atchDataSize should be 0!
       
   268 		}
       
   269 	
       
   270 	return err;
       
   271 	}
       
   272 
       
   273 EXPORT_C TInt TImAttachmentFile::WriteFile (const TDesC16& aBuffer)
       
   274 	{
       
   275 	// Write data from aBuffer to file using an intermediate buffer, iDataLine. 
       
   276 	// This is to reduce the number of file writes. 'Large' files can be written 
       
   277 	// directly to the file.
       
   278 	
       
   279 	iFileText.Set(iFile);
       
   280 	TInt err (KErrNone);
       
   281 
       
   282 	TInt atchDataSize = aBuffer.Length();
       
   283 	TInt bufferSize = iDataLine.Length();
       
   284 	TInt spaceOnBuffer = 0;
       
   285 	TInt bufPos = 0;
       
   286 	// Fill buffer
       
   287 	spaceOnBuffer = KDataLineBufferLength - bufferSize;
       
   288 
       
   289 	// if incoming data is bigger than buffer 
       
   290 	// we can write it directly to file if packing is not done earier
       
   291 	if (atchDataSize >= KDataLineBufferLength && spaceOnBuffer == KDataLineBufferLength)
       
   292 		{
       
   293 		err=iFileText.Write(aBuffer.Mid(bufPos));
       
   294 		atchDataSize=0;
       
   295 		return err;
       
   296 		}
       
   297 
       
   298 	if (atchDataSize > spaceOnBuffer)
       
   299 		{
       
   300 		// The remaining size of the atch is greater than the buffer
       
   301 		// Append as much data from the atch than will fit in the buffer
       
   302 		iDataLine.Append(aBuffer.Mid(bufPos,spaceOnBuffer));
       
   303 
       
   304 		bufPos += spaceOnBuffer;
       
   305 		atchDataSize -= spaceOnBuffer;
       
   306 
       
   307 		// Flush buffer to file
       
   308 		err = WriteFile();
       
   309 		iDataLine.SetLength(0);
       
   310 		bufferSize = 0;
       
   311 		
       
   312 		// if the size of the attachment is 'large' then write the whole file
       
   313 		if (atchDataSize >= KDataLineBufferLength)
       
   314 			{
       
   315 			err=iFileText.Write(aBuffer.Mid(bufPos));
       
   316 			atchDataSize=0;
       
   317 			}
       
   318 		else 
       
   319 			{
       
   320 			iDataLine.Append(aBuffer.Mid(bufPos,atchDataSize));
       
   321 			bufPos += atchDataSize;
       
   322 			atchDataSize -= atchDataSize;	// atchDataSize should be 0!
       
   323 			}
       
   324 		}
       
   325 	else 
       
   326 		{
       
   327 		iDataLine.Append(aBuffer.Mid(bufPos,atchDataSize));
       
   328 		bufPos += atchDataSize;
       
   329 		atchDataSize -= atchDataSize;	// atchDataSize should be 0!
       
   330 		}
       
   331 	
       
   332 	return err;
       
   333 	}
       
   334 
       
   335 
       
   336 TInt TImAttachmentFile::WriteFile ()
       
   337 	{
       
   338 	return iFile.Write(iDataLine);
       
   339 	}
       
   340 
       
   341 EXPORT_C TInt TImAttachmentFile::CloseFile()
       
   342 	{
       
   343 	// Check if writing to a file, in which case make sure iDataLine buffer..
       
   344 	// has been flushed.
       
   345 	TInt err = KErrNone;
       
   346 	if (iWritingToFile)
       
   347 		err = FlushFile();
       
   348 	Reset();
       
   349 	iFile.Close();
       
   350 	return err;
       
   351 	}
       
   352 
       
   353 TInt TImAttachmentFile::FlushFile()
       
   354 	{
       
   355 	if (iDataLine.Length() > 0)
       
   356 		{
       
   357 		WriteFile();
       
   358 		}
       
   359 	return iFile.Flush();	
       
   360 	}
       
   361 
       
   362 EXPORT_C TInt TImAttachmentFile::DeleteAttachment(const TDesC& aDirPath, const TDesC& aFileName)
       
   363 	{
       
   364 	TInt err(AppendValidFile(aDirPath, aFileName, iFullFilePath));
       
   365 	if (err!=KErrNone)
       
   366 		return err;
       
   367 	return (iFileSession.Delete(iFullFilePath));
       
   368 	}
       
   369 
       
   370 TInt TImAttachmentFile::AppendValidFile(const TDesC& aDirPath, const TDesC& aFileName, TFileName& rFullFilePath)
       
   371 	{	
       
   372 	// if the resultant filename is too long, report it,
       
   373 	// trimming the filename is not an option, apparently.
       
   374 
       
   375 	rFullFilePath=aDirPath;
       
   376 	if (aDirPath.Length() + aFileName.Length() > KMaxFileName)
       
   377 		{
       
   378 		return KErrBadName;
       
   379 		}
       
   380 	else
       
   381 		{
       
   382 		rFullFilePath.Append(aFileName);
       
   383 		}
       
   384 	return KErrNone;	
       
   385 	}
       
   386