email/pop3andsmtpmtm/servermtmutils/src/cimcaf.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2004-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 // Implementation of the CImCaf class.
       
    15 // 
       
    16 //
       
    17 /**
       
    18  @file
       
    19  @internalTechnology 
       
    20 */
       
    21 
       
    22 #include <cimcaf.h>
       
    23 #include <tmsvpackednotifierrequest.h>
       
    24 
       
    25 _LIT8(KXHyphen,"X-");
       
    26 _LIT8(KContentHyphen,"Content-");
       
    27 
       
    28 /**
       
    29 Construct with callers file session handle.
       
    30 c'tor
       
    31 */
       
    32 EXPORT_C CImCaf::CImCaf(RFs& aFs) : iFsSession(aFs)
       
    33 	{}
       
    34 
       
    35 /**
       
    36 d'tor
       
    37 */
       
    38 EXPORT_C  CImCaf::~CImCaf()
       
    39 	{
       
    40 	delete iSupplier;
       
    41 	delete iContentType;
       
    42 	delete iMetaData;
       
    43 	delete iImportFile;
       
    44 	}
       
    45 	
       
    46 
       
    47 /**
       
    48 Prior to StartProcessing() the CAF agent could provide an initial filename for the CAF session.
       
    49 @param aAttachmentFileName Descriptor for writing CAF agent suggested filename.
       
    50 @return KErrNone success or system error code.
       
    51 @pre PrepareProcessingL() must be called prior to this call.
       
    52 @panic imut 36
       
    53 */
       
    54 EXPORT_C TInt CImCaf::GetSuggestedAttachmentFileName(TDes& aAttachmentFileName) const
       
    55 	{
       
    56 	__ASSERT_DEBUG(iImportFile != NULL, gPanic(EPanicInvalidCafState));
       
    57 	return iImportFile->GetSuggestedOutputFileName(aAttachmentFileName);
       
    58 	}
       
    59 
       
    60 
       
    61 /**
       
    62 Write a buffer of data to the CAF Agent.
       
    63 @param aData Data to write to the CAF agent.
       
    64 @return KErrNone success or system error code.
       
    65 @pre StartProcessingL() must be called prior to this call
       
    66 @post Generates 0-N receipt files under the current message attachment entry.
       
    67 @panic imut 36
       
    68 */
       
    69 EXPORT_C TInt CImCaf::WriteData(const TDesC8& aData)
       
    70 	{
       
    71 	TInt err(KErrNone);
       
    72 	__ASSERT_DEBUG(iImportFile != NULL, gPanic(EPanicInvalidCafState));
       
    73 	err = iImportFile->WriteData(aData);
       
    74 	__ASSERT_DEBUG(err == KErrNone || err == KErrNotReady || err == KErrCANewFileHandleRequired, gPanic(EPanicInvalidCafState));
       
    75 	if(err == KErrNone || err == KErrNotReady)
       
    76 		{		
       
    77 		// Write succeeded
       
    78 		return err;
       
    79 		}
       
    80 	else if(err == KErrCANewFileHandleRequired)
       
    81 		{
       
    82 		// We need a new file handle see if the class member is set up ready
       
    83 		if(iAttachmentFile.SubSessionHandle())
       
    84 			{
       
    85 			// Caller provided open file handle
       
    86 			err = iImportFile->ContinueWithNewOutputFile(iAttachmentFile,iAttachmentFilePath);
       
    87 			// Close the caller's file handle as CAF has made a copy
       
    88 			iAttachmentFile.Close();
       
    89 			if(err == KErrCANewFileHandleRequired)
       
    90 				TRAP(err,NewFileL());
       
    91 			}
       
    92 		else
       
    93 			{
       
    94 			TRAP(err,NewFileL());
       
    95 			}
       
    96 		// New file handle is created, write data
       
    97 		err = iImportFile->WriteData(aData);
       
    98 		}
       
    99 	__ASSERT_DEBUG(err == KErrNone, gPanic(EPanicInvalidCafState));
       
   100 	return err;
       
   101 	}
       
   102 
       
   103 /**
       
   104 Add Mime header metadata to CAF class in preparation for PrepareProcessingL()
       
   105 Metadata comprises a field and data pair. For example:
       
   106 aField - "X-Oma-Drm-Separate-Delivery" aData - "12"
       
   107 If field does not contain "X-" or "Content-" then the method returns without
       
   108 adding them to the class.
       
   109 @param aField - Mime header field
       
   110 @param aData - Mime field's corresponding data
       
   111 @panic imut 36
       
   112 */
       
   113 EXPORT_C void CImCaf::AddToMetaDataL(const TDesC8& aField, const TDesC8& aData)
       
   114 	{
       
   115 	if(aField.FindF(KContentHyphen) == KErrNone && aField.FindF(KXHyphen) == KErrNone)
       
   116 		{		
       
   117 		return;
       
   118 		}
       
   119 	if(!iMetaData)
       
   120 		{		
       
   121 		iMetaData = CMetaDataArray::NewL();
       
   122 		}
       
   123 	// Check we're not overwriting an existing one. Could show up a logic error.
       
   124 	#if (defined _DEBUG)
       
   125 	TPtrC8 des(iMetaData->SearchL(aField));
       
   126 	__ASSERT_DEBUG(des.Length() == 0, gPanic(EPanicInvalidCafState));	
       
   127 	#endif
       
   128 	iMetaData->AddL(aField,aData);
       
   129 	}
       
   130 
       
   131 /**
       
   132 Add Mime header metadata to CAF class in preparation for PrepareProcessingL()
       
   133 Parses a complete unfolded mime header line to extract fields and values
       
   134 Format expected:
       
   135 Fieldxxx: Valuexxx;ParamField=ParamValue
       
   136 If line does not contain "X-" or "Content-" then the method returns without
       
   137 adding data to the class.
       
   138 @param aMimeLine - Mime header data line minus CRLF
       
   139 @panic imut 36
       
   140 */
       
   141 EXPORT_C void CImCaf::AddToMetaDataL(const TDesC8& aMimeLine)
       
   142 	{
       
   143 	// Don't bother adding if it's neither of these
       
   144 	// This means we won't add envelope stuff
       
   145 	if(aMimeLine.FindF(KContentHyphen) == KErrNone && aMimeLine.FindF(KXHyphen) == KErrNone)
       
   146 		{
       
   147 		return;
       
   148 		}
       
   149 	if(!iMetaData)
       
   150 		{		
       
   151 		iMetaData = CMetaDataArray::NewL();
       
   152 		}
       
   153 	TInt offset;
       
   154 	TPtrC8 field;
       
   155 	TPtrC8 data;
       
   156 	// Isolate the field and data pair separated by ':'	
       
   157 	if((offset = aMimeLine.Locate(KImcvColon)) != KErrNotFound)
       
   158 		{
       
   159 		field.Set(aMimeLine.Left(++offset));
       
   160 		// Remove leading whitespace
       
   161 		while(offset < aMimeLine.Length() && aMimeLine[offset] == ' ')
       
   162 			{
       
   163 			offset++;
       
   164 			}
       
   165 		if(offset < aMimeLine.Length())
       
   166 			{
       
   167 			data.Set(aMimeLine.Mid(offset));
       
   168 			}
       
   169 		else
       
   170 			return;
       
   171 		}
       
   172 	else
       
   173 		{
       
   174 		// No colon should never happen if mail is not corrupt
       
   175 		__ASSERT_DEBUG(offset != KErrNotFound, gPanic(EPanicInvalidCafState));
       
   176 		return;
       
   177 		}
       
   178 	// Check we're not overwriting an existing one. Could show up a logic error.
       
   179 	#if (defined _DEBUG)
       
   180 	TPtrC8 des(iMetaData->SearchL(field));
       
   181 	__ASSERT_DEBUG(des.Length() == 0, gPanic(EPanicInvalidCafState));				
       
   182 	#endif
       
   183 	iMetaData->AddL(field,data);
       
   184 	}
       
   185 
       
   186 /**
       
   187 Prepare the CImCAF class for a CAF session.
       
   188 @pre RegisterL() must be called prior to this call and success checked with a Registered() call.
       
   189 @panic imut 36
       
   190 */
       
   191 EXPORT_C void CImCaf::PrepareProcessingL()
       
   192 	{
       
   193 	__ASSERT_DEBUG(iSupplier != NULL && iContentType != NULL && iMetaData != NULL && iImportFile == NULL, gPanic(EPanicInvalidCafState));
       
   194 	iImportFile = iSupplier->ImportFileL(iContentType->Des(),*iMetaData);// Prepare agent for writing data.
       
   195 	Deregister();
       
   196 	}
       
   197 	
       
   198 /**
       
   199 Set the CAF class variables required to Start the CAF write session.
       
   200 @param aDefaultAttachmentFileName Localised default attachment name.
       
   201 @param aAttachmentFilePath If CAF agent requires extra files, this is the folder/attachment entry path.
       
   202 @param aServerEntry Pointer to the message store attachment entry for the CAF session.
       
   203 @param aStartFile An open File handle for attachment write. Caller can close following this call.
       
   204 @pre PrepareProcessingL() must be called prior to this call
       
   205 @pre aServerEntry aServerEntry is set to the attachment entry id and remains set there for the duration of the CAF session
       
   206 @panic imut 36
       
   207 */
       
   208 EXPORT_C void CImCaf::StartProcessing(const TDesC& aDefaultAttachmentFileName,const TDesC& aAttachmentFilePath,CMsvServerEntry& aServerEntry,RFile& aStartFile)
       
   209 	{
       
   210 	__ASSERT_DEBUG(iSupplier != NULL && iImportFile != NULL && iContentType == NULL && iMetaData == NULL && aStartFile.SubSessionHandle() != 0, gPanic(EPanicInvalidCafState));
       
   211 	iDefaultAttachmentFileName.Set(aDefaultAttachmentFileName);
       
   212 	iAttachmentFilePath = aAttachmentFilePath;
       
   213 	iServerEntry = &aServerEntry;
       
   214 	iAttachmentFile.Duplicate(aStartFile); // Caller can close their copy
       
   215 	}
       
   216 	
       
   217 /**
       
   218 Terminate CAF session.
       
   219 Check the attachments under the attachment entry and see if there are any rights receipts.
       
   220 Set the mime type to a unique CAF one if there are.
       
   221 @panic imut 36
       
   222 @pre iServerEntry set to the attachment entry which owns the attachment
       
   223 */
       
   224 EXPORT_C void CImCaf::EndProcessingL()
       
   225 	{
       
   226 	__ASSERT_DEBUG(iImportFile != NULL, gPanic(EPanicInvalidCafState));
       
   227 	WriteDataCompleteL();
       
   228 	CMsvStore* store = iServerEntry->EditStoreL(); 
       
   229 	CleanupStack::PushL(store);
       
   230 	// Loop through the attachments under the current attachment entry
       
   231 	for(TInt i=0;i<store->AttachmentManagerL().AttachmentCount();i++)
       
   232 		{
       
   233 		CMsvAttachment* attachment = store->AttachmentManagerL().GetAttachmentInfoL(i);
       
   234 		// Loop through the output files and see if there's a receipt that matches the attachment
       
   235 		CleanupStack::PushL(attachment);
       
   236 		TInt j;
       
   237 		for(j=0;j<iImportFile->OutputFileCountL();j++)
       
   238 			{
       
   239 			CSupplierOutputFile& outputFile = iImportFile->OutputFileL(j);
       
   240 			if(outputFile.OutputType() == EReceipt)
       
   241 				{
       
   242 				if(attachment->FilePath().MatchF(outputFile.FileName()) != KErrNotFound)
       
   243 					{
       
   244 					// Matched so write the Receipt indicator to the attachment and save it
       
   245 					// ModifyAttachmentInfoL() takes ownership of the CMsvAttachment 
       
   246 					attachment->SetMimeTypeL(KEpocMimeTypeDrm);
       
   247 					store->AttachmentManagerExtensionsL().ModifyAttachmentInfoL(attachment);
       
   248 					CleanupStack::Pop(attachment);
       
   249 					break;
       
   250 					}
       
   251 				}
       
   252 			}
       
   253 		// If loop did not break then there was no match so destroy.
       
   254 		if(j==iImportFile->OutputFileCountL())
       
   255 			{
       
   256 			CleanupStack::PopAndDestroy(attachment);
       
   257 			}
       
   258 		}
       
   259 	store->CommitL();
       
   260 	CleanupStack::PopAndDestroy(store);
       
   261 
       
   262 	delete iImportFile;
       
   263 	iImportFile = NULL;
       
   264 	}
       
   265 
       
   266 /**
       
   267 Attempt to register a mime content-type with a CAF agent.
       
   268 Success can be checked by a subsequent call to Registered().
       
   269 @param aMimeLine MIME Content-Type for possible interest by a CAF agent. For example - application/vnd.oma.drm.rights+xml
       
   270 @panic imut 36
       
   271 */
       
   272 EXPORT_C void CImCaf::RegisterL(const TDesC8& aMimeLine)
       
   273 	{
       
   274 	// There's no nested CAF registration so iContentType instance should always be NULL
       
   275 	__ASSERT_DEBUG(iContentType == NULL, gPanic(EPanicInvalidCafState));
       
   276 	if(!iSupplier)
       
   277 		{		
       
   278 		iSupplier = CSupplier::NewL();
       
   279 		}
       
   280 	if(iSupplier->IsImportSupported(aMimeLine))
       
   281 		{
       
   282 		// CAF agent interested in the content-type
       
   283 		// Use this buffer for storing the content-type.
       
   284 		// We could extract it from the caf metadata later but this will be quicker
       
   285 		iContentType = aMimeLine.AllocL();
       
   286 		// REMOVE when CAF fixed
       
   287 		iContentType->Des().LowerCase();
       
   288 		// Construct the metadata array here but add to it later
       
   289 		// Could contain stuff like "X-Oma-Drm-Separate-Delivery: 12"
       
   290 		if(!iMetaData)
       
   291 			{		
       
   292 			iMetaData = CMetaDataArray::NewL();
       
   293 			}
       
   294 		}
       
   295 	}
       
   296 
       
   297 /**
       
   298 Free resources allocated during CAF session
       
   299 @pre RegisterL() must be called prior to this call.
       
   300 @panic imut 36
       
   301 */
       
   302 EXPORT_C void CImCaf::Deregister()
       
   303 	{
       
   304 	__ASSERT_DEBUG(Registered(), gPanic(EPanicInvalidCafState));
       
   305 	// Don't delete iSupplier. This is kept open to prevent reloading of DLLs
       
   306 	delete iContentType;
       
   307 	iContentType = NULL;
       
   308 	delete iMetaData;
       
   309 	iMetaData = NULL;
       
   310 	}
       
   311 		
       
   312 /**
       
   313 Check whether RegisterL() succeeded.
       
   314 @return Registered status of the CAF instance.
       
   315 */
       
   316 EXPORT_C TBool CImCaf::Registered() const
       
   317 	{
       
   318 	return iContentType != NULL;
       
   319 	}
       
   320 	
       
   321 /**
       
   322 Retrieve processing status for the CAF session.
       
   323 @return Processing status of the CAF instance
       
   324 */
       
   325 EXPORT_C TBool CImCaf::Processing() const
       
   326 	{
       
   327 	return iImportFile != NULL;
       
   328 	}
       
   329 
       
   330 //Tell the CAF agent this that the last data for the CAF session has been written.
       
   331 void CImCaf::WriteDataCompleteL()
       
   332 	{
       
   333 	TInt err(KErrNone);
       
   334 	// Expect KErrNone or KErrCANewFileHandleRequired
       
   335 	err = iImportFile->WriteDataComplete();
       
   336 	__ASSERT_DEBUG(err == KErrNone || err == KErrCANewFileHandleRequired,gPanic(EPanicInvalidCafState));	
       
   337 	if(err == KErrCANewFileHandleRequired)
       
   338 		{
       
   339 		// CAF framework requires another file handle to finish the session
       
   340 		NewFileL();
       
   341 		}
       
   342 	else
       
   343 		{
       
   344 		User::LeaveIfError(err);
       
   345 		}
       
   346 	}
       
   347 
       
   348 // Gets an output file for continued write by the framework.
       
   349 // To be called when CAF framework has returned KErrCANewFileHandleRequired
       
   350 // from a WriteData() or WriteDataComplete() call.
       
   351 // Creates 1-N new CAF files under the session attachment entry
       
   352 void CImCaf::NewFileL()
       
   353 	{
       
   354 	// Our iServerEntry will be referencing the current session attachment entry
       
   355 	CMsvStore* store = iServerEntry->EditStoreL(); 
       
   356 	CleanupStack::PushL(store);
       
   357 
       
   358 	TInt err(KErrNone);
       
   359 	TFileName filename;
       
   360 	// Loop creating files until success or error
       
   361 	do
       
   362 		{
       
   363 		// See if the framework can suggest a filename
       
   364 		// Failing that see if it can suggest an extension
       
   365 		// Use the default filename if we have to
       
   366 		if((err = iImportFile->GetSuggestedOutputFileName(filename)) == KErrUnknown)
       
   367 			{
       
   368 			TFileName ext;
       
   369 			filename = iDefaultAttachmentFileName;
       
   370 			if(iImportFile->GetSuggestedOutputFileExtension(ext) == KErrNone)
       
   371 				{				
       
   372 				filename.Append(ext);				
       
   373 				}
       
   374 			}
       
   375 		__ASSERT_ALWAYS(err == KErrUnknown || err == KErrNone,gPanic(EPanicInvalidCafState));
       
   376 		CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
       
   377 		// Ok to use stack file handle because agent clones it
       
   378 		RFile file;
       
   379 		store->CreateShareProtectedAttachmentL(filename,file,attachment);
       
   380 		err = iImportFile->ContinueWithNewOutputFile(file,attachment->FilePath());
       
   381 		__ASSERT_ALWAYS(err == KErrNone || err == KErrCANewFileHandleRequired,gPanic(EPanicInvalidCafState));
       
   382 		file.Close(); // Framework clones the file handle so close it
       
   383 		}while (err == KErrCANewFileHandleRequired);
       
   384 	store->CommitL();
       
   385 	CleanupStack::PopAndDestroy(store);
       
   386 	}