email/pop3andsmtpmtm/clientmtms/src/cimattachmentmanager.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 //
       
    15 
       
    16 #include "cimattachmentmanager.h"
       
    17 
       
    18 CImAttachmentManager::CImAttachmentManager(CImEmailMessage& aEmailMessage,CMsvEntry& aEntry) :CActive(EPriorityStandard),iClientEntry(aEntry), iEmailMessage(aEmailMessage) 
       
    19 	{
       
    20 	CActiveScheduler::Add(this);
       
    21 	}
       
    22 
       
    23 void CImAttachmentManager::ConstructL()
       
    24 	{
       
    25 	}
       
    26 	
       
    27 CImAttachmentManager::~CImAttachmentManager()
       
    28 	{
       
    29 	delete iStore;
       
    30 	iAttachmentInfoList.ResetAndDestroy();
       
    31 	delete iMsvOperation;
       
    32 	delete iEntry;
       
    33 	}
       
    34 	
       
    35 CImAttachmentManager* CImAttachmentManager::NewL(CImEmailMessage& aEmailMessage,CMsvEntry& aEntry)
       
    36 	{
       
    37 	CImAttachmentManager* self = new(ELeave) CImAttachmentManager(aEmailMessage,aEntry);
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 	
       
    44 void CImAttachmentManager::RunL()
       
    45 	{
       
    46 	switch(iAttachmentManagerState)
       
    47 		{
       
    48 	case EModifyAttachmentInfo:
       
    49 		{
       
    50 		if(!iStore == NULL)
       
    51 			{
       
    52 			ModifyAttachmentInfoL();
       
    53 			}
       
    54 		ChangeEntryDetailsL();
       
    55 		iAttachmentManagerState = EChangeEntryDetails;
       
    56 		} 
       
    57 		break;
       
    58 	case EChangeEntryDetails:
       
    59 		{
       
    60 		delete iEntry;
       
    61 		iEntry = NULL;
       
    62 
       
    63 		User::RequestComplete(iReportStatus, iStatus.Int());
       
    64 		}
       
    65 		break;
       
    66 	default:
       
    67 		User::Leave(KErrNotSupported);
       
    68 		break;
       
    69 		};
       
    70 	}
       
    71 	
       
    72 void CImAttachmentManager::DoCancel()
       
    73 	{
       
    74 	if (iMsvOperation)
       
    75 		iMsvOperation->Cancel();
       
    76 
       
    77 	delete iStore;
       
    78 	iStore = NULL;
       
    79 
       
    80 	delete iEntry;
       
    81 	iEntry = NULL;
       
    82 	}
       
    83 
       
    84 TInt CImAttachmentManager::RunError(TInt aError)
       
    85 	{
       
    86 	if(iStore != NULL)
       
    87 		{
       
    88 		delete iStore;
       
    89 		iStore = NULL;	
       
    90 		}
       
    91 	
       
    92 	delete iEntry;
       
    93 	iEntry = NULL;
       
    94 
       
    95 	User::RequestComplete(iReportStatus, aError);
       
    96 	return KErrNone;	
       
    97 	}
       
    98 	
       
    99 void CImAttachmentManager::AddAttachmentL(const TDesC& aFilePath, CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   100 	{
       
   101 	iEmailMessage.AddAttachmentL(aFilePath,aAttachmentInfo,aStatus);
       
   102 	}
       
   103 
       
   104 void CImAttachmentManager::AddAttachmentL(RFile& aFileHandle, CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   105 	{
       
   106 	iEmailMessage.AddAttachmentL(aFileHandle,aAttachmentInfo,aStatus);
       
   107 	}
       
   108 
       
   109 void CImAttachmentManager::AddLinkedAttachmentL(const TDesC& aFilePath, CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   110 	{
       
   111 	iEmailMessage.AddLinkedAttachmentL(aFilePath,aAttachmentInfo,aStatus);
       
   112 	}
       
   113 
       
   114 void CImAttachmentManager::AddEntryAsAttachmentL(TMsvId aEntryId, CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   115 	{
       
   116 	iEmailMessage.AddEntryAsAttachmentL(aEntryId,aAttachmentInfo,aStatus);
       
   117 	}
       
   118 
       
   119 void CImAttachmentManager::CreateAttachmentL(const TDesC& aFileName, RFile& aAttachmentFile, CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   120 	{
       
   121 	iEmailMessage.CreateAttachmentL(aFileName,aAttachmentFile,aAttachmentInfo,aStatus);
       
   122 	}
       
   123 
       
   124 /**
       
   125 This API not supported.
       
   126 Leaves with KErrNotSupported.
       
   127 */
       
   128 void CImAttachmentManager::RenameAttachmentL(TInt /*aIndex*/, const TDesC& /*aNewName*/, TRequestStatus& /*aStatus*/)
       
   129 	{
       
   130 	User::Leave(KErrNotSupported);
       
   131 	}
       
   132 
       
   133 TInt CImAttachmentManager::AttachmentCount() const
       
   134 	{
       
   135 	return iAttachmentInfoList.Count();
       
   136 	}
       
   137 
       
   138 CMsvAttachment* CImAttachmentManager::GetAttachmentInfoL(TInt aIndex)
       
   139 	{
       
   140 	__ASSERT_ALWAYS(iAttachmentInfoList.Count() || (aIndex >= 0 && aIndex < iAttachmentInfoList.Count()),User::Invariant());
       
   141 	
       
   142 	CMsvAttachment* attachmentInfo = iAttachmentInfoList[aIndex];
       
   143 	return CMsvAttachment::NewL(*attachmentInfo);
       
   144 	}
       
   145 
       
   146 CMsvAttachment* CImAttachmentManager::GetAttachmentInfoL(TMsvAttachmentId aId)
       
   147 	{
       
   148 	TInt index = AttachmentIndexInMessage(aId);
       
   149 	User::LeaveIfError(index);
       
   150 	
       
   151 	CMsvAttachment* attachmentInfo = iAttachmentInfoList[index];
       
   152 	return CMsvAttachment::NewL(*attachmentInfo);	
       
   153 	}
       
   154 
       
   155 RFile CImAttachmentManager::GetAttachmentFileL(TInt aIndex)
       
   156 	{
       
   157 	__ASSERT_ALWAYS(iAttachmentInfoList.Count() || (aIndex >= 0 && aIndex < iAttachmentInfoList.Count()),User::Invariant());
       
   158 	// Need to reset the client entry to the owning attachment entry's TMsvId
       
   159 	TInt entryId;
       
   160 	// Stored in the integer attributes for the CMsvAttachment class
       
   161 	if(iAttachmentInfoList[aIndex]->GetIntAttribute(KUidMsvEmailAttachmentEntryId,entryId) != KErrNone)
       
   162 		{
       
   163 	// We're moving from the per-message iAttachmentInfoList to the per attachment entry list
       
   164 		entryId = iAttachmentInfoList[aIndex]->Id();
       
   165 		}
       
   166 	
       
   167 	CMsvEntry* entry=CMsvEntry::NewL(iClientEntry.Session(), entryId, TMsvSelectionOrdering());
       
   168 	CleanupStack::PushL(entry);
       
   169 
       
   170 	CMsvStore* store = entry->ReadStoreL();
       
   171 	CleanupStack::PushL(store);
       
   172 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   173 	// We're on the attachment entry but we need the index into that array
       
   174 	// This is stored in another attribute 
       
   175 	RFile file;
       
   176 
       
   177 	TInt entryIndex;
       
   178 	if(iAttachmentInfoList[aIndex]->GetIntAttribute(KUidMsvEmailAttachmentEntryIndex,entryIndex) == KErrNone)
       
   179 		{
       
   180 	// Home in on the original CMsvAttachment
       
   181 		file = attachmentMgr.GetAttachmentFileL(entryIndex);
       
   182 		}
       
   183 	else
       
   184 		{
       
   185 		file = attachmentMgr.GetAttachmentFileL(0);
       
   186 		}
       
   187 	CleanupStack::PopAndDestroy(2,entry);//store, entry
       
   188 
       
   189 	return file;
       
   190 	}
       
   191 
       
   192 RFile CImAttachmentManager::GetAttachmentFileL(TMsvAttachmentId aId)
       
   193 	{
       
   194 	// Lookup in the per-message array and extract the index
       
   195 	TInt index = AttachmentIndexInMessage(aId);
       
   196 	User::LeaveIfError(index);
       
   197 	
       
   198 	// Need to reset the client entry to the owning attachment entry's TMsvId
       
   199 	TInt entryId;
       
   200 	// Stored in the integer attributes for the CMsvAttachment class
       
   201 	if(iAttachmentInfoList[index]->GetIntAttribute(KUidMsvEmailAttachmentEntryId,entryId) != KErrNone)
       
   202 	// We're moving from the per-message iAttachmentInfoList to the per attachment entry list
       
   203 		{
       
   204 		entryId = iAttachmentInfoList[index]->Id();
       
   205 		}
       
   206 
       
   207 	CMsvEntry* entry=CMsvEntry::NewL(iClientEntry.Session(), entryId, TMsvSelectionOrdering());
       
   208 	CleanupStack::PushL(entry);
       
   209 		
       
   210 	CMsvStore* store = entry->ReadStoreL();
       
   211 	CleanupStack::PushL(store);
       
   212 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   213 	// We're on the attachment entry but we need the index into that array
       
   214 	// This is stored in another attribute 
       
   215 	RFile file;
       
   216 
       
   217 	TInt entryIndex;
       
   218 	if(iAttachmentInfoList[index]->GetIntAttribute(KUidMsvEmailAttachmentEntryIndex,entryIndex) == KErrNone)
       
   219 		{
       
   220 	// Home in on the original CMsvAttachment
       
   221 		file = attachmentMgr.GetAttachmentFileL(entryIndex);
       
   222 		}
       
   223 	else
       
   224 		{
       
   225 		file = attachmentMgr.GetAttachmentFileL(0);
       
   226 		}
       
   227 	CleanupStack::PopAndDestroy(2,entry);//store, entry
       
   228 
       
   229 	return file;
       
   230 	}
       
   231 	
       
   232 RFile CImAttachmentManager::GetAttachmentFileForWriteL(TInt aIndex)
       
   233 	{
       
   234 	__ASSERT_ALWAYS(iAttachmentInfoList.Count() || (aIndex >= 0 && aIndex < iAttachmentInfoList.Count()),User::Invariant());
       
   235 	// Need to reset the client entry to the owning attachment entry's TMsvId
       
   236 	TInt entryId;
       
   237 	// Stored in the integer attributes for the CMsvAttachment class
       
   238 	if(iAttachmentInfoList[aIndex]->GetIntAttribute(KUidMsvEmailAttachmentEntryId,entryId) != KErrNone)
       
   239 		{
       
   240 	// We're moving from the per-message iAttachmentInfoList to the per attachment entry list
       
   241 		entryId = iAttachmentInfoList[aIndex]->Id();
       
   242 		}
       
   243 
       
   244 	CMsvEntry* entry=CMsvEntry::NewL(iClientEntry.Session(), entryId, TMsvSelectionOrdering());
       
   245 	CleanupStack::PushL(entry);
       
   246 	
       
   247 	CMsvStore* store = entry->EditStoreL();
       
   248 	CleanupStack::PushL(store);
       
   249 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   250 	// We're on the attachment entry but we need the index into that array
       
   251 	// This is stored in another attribute 
       
   252 	RFile file;
       
   253 
       
   254 	TInt entryIndex;
       
   255 	if(iAttachmentInfoList[aIndex]->GetIntAttribute(KUidMsvEmailAttachmentEntryIndex,entryIndex) == KErrNone)
       
   256 		{
       
   257 	// Home in on the original CMsvAttachment
       
   258 		file = attachmentMgr.GetAttachmentFileForWriteL(entryIndex);
       
   259 		}
       
   260 	else
       
   261 		{
       
   262 		file = attachmentMgr.GetAttachmentFileForWriteL(0);
       
   263 		}
       
   264 	CleanupStack::PopAndDestroy(2, entry);//store, entry
       
   265 	
       
   266 	return file;
       
   267 	}
       
   268 
       
   269 RFile CImAttachmentManager::GetAttachmentFileForWriteL(TMsvAttachmentId aId)
       
   270 	{
       
   271 	// Lookup in the per-message array and extract the index
       
   272 	TInt index = AttachmentIndexInMessage(aId);
       
   273 	User::LeaveIfError(index);
       
   274 
       
   275 
       
   276 	// Need to reset the client entry to the owning attachment entry's TMsvId
       
   277 	TInt entryId;
       
   278 	// Stored in the integer attributes for the CMsvAttachment class
       
   279 	if(iAttachmentInfoList[index]->GetIntAttribute(KUidMsvEmailAttachmentEntryId,entryId) != KErrNone)
       
   280 		{
       
   281 		// We're moving from the per-message iAttachmentInfoList to the per attachment entry list
       
   282 		entryId = iAttachmentInfoList[index]->Id();
       
   283 		}
       
   284 
       
   285 	CMsvEntry* entry=CMsvEntry::NewL(iClientEntry.Session(), entryId, TMsvSelectionOrdering());
       
   286 	CleanupStack::PushL(entry);
       
   287 		
       
   288 	CMsvStore* store = entry->EditStoreL();
       
   289 	CleanupStack::PushL(store);
       
   290 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   291 	// We're on the attachment entry but we need the index into that array
       
   292 	// This is stored in another attribute 
       
   293 	RFile file;
       
   294 
       
   295 	TInt entryIndex;
       
   296 	if(iAttachmentInfoList[index]->GetIntAttribute(KUidMsvEmailAttachmentEntryIndex,entryIndex) == KErrNone)
       
   297 		{
       
   298 	// Home in on the original CMsvAttachment
       
   299 		file = attachmentMgr.GetAttachmentFileForWriteL(entryIndex);
       
   300 		}
       
   301 	else
       
   302 		{
       
   303 		file = attachmentMgr.GetAttachmentFileForWriteL(0);
       
   304 		}
       
   305 	CleanupStack::PopAndDestroy(2,entry);//entry, store
       
   306 	
       
   307 	return file;
       
   308 	}
       
   309 
       
   310 void CImAttachmentManager::RemoveAttachmentL(TInt aIndex, TRequestStatus& aStatus)
       
   311 	{	
       
   312 	__ASSERT_ALWAYS(aIndex >= 0 && aIndex < iAttachmentInfoList.Count(),User::Invariant());
       
   313 	
       
   314 	RemoveAttachmentL(iAttachmentInfoList[aIndex]->Id(),aStatus);
       
   315 	}
       
   316 
       
   317 void CImAttachmentManager::RemoveAttachmentL(TMsvAttachmentId aId, TRequestStatus& aStatus)
       
   318 	{
       
   319 	iEmailMessage.RemoveAttachmentL(aId,aStatus);
       
   320 	}
       
   321 
       
   322 void CImAttachmentManager::CancelRequest()
       
   323 	{
       
   324 	iEmailMessage.Cancel();
       
   325 	}
       
   326 	
       
   327 void CImAttachmentManager::ModifyAttachmentInfoL(CMsvAttachment* aAttachmentInfo, TRequestStatus& aStatus)
       
   328 	{
       
   329 	TInt index = AttachmentIndexInMessage(aAttachmentInfo->Id());
       
   330 	User::LeaveIfError(index);
       
   331 	
       
   332 	iAttachmentManagerState = EModifyAttachmentInfo;
       
   333 
       
   334 	delete iEntry;
       
   335 	iEntry = NULL;
       
   336 	
       
   337 	iEntry=CMsvEntry::NewL(iClientEntry.Session(), iAttachmentInfoList[index]->Id(), TMsvSelectionOrdering());
       
   338 	iStore = iEntry->EditStoreL();
       
   339 
       
   340 	MMsvAttachmentManager& attachmentMgr = iStore->AttachmentManagerL();
       
   341 
       
   342 	aStatus = KRequestPending;
       
   343 	iReportStatus=&aStatus;
       
   344 	
       
   345 	// The id stored in the Attachment info list in CImemailmessage is the 
       
   346 	// attachment entry id, this is not the actual id of the attachment
       
   347 	// Reset the id as 1 , as emails have one attachment per attachment entry.
       
   348 	aAttachmentInfo->SetId(1);
       
   349 	attachmentMgr.ModifyAttachmentInfoL(aAttachmentInfo,iStatus);
       
   350 	SetActive();
       
   351 	}	
       
   352 
       
   353 void CImAttachmentManager::LoadL() 
       
   354 	{
       
   355 	// Flush the array before loading
       
   356 	iAttachmentInfoList.ResetAndDestroy();
       
   357 	
       
   358 	// Load the attachments only if it was a message entry
       
   359 	const TMsvEmailEntry& entry=iClientEntry.Entry();
       
   360 	if(entry.iType==KUidMsvMessageEntry)
       
   361 		{
       
   362 		// CImEmailMessage fills  the attachment list for CImAttachmentManager::iAttachmentInfoList
       
   363 		// get the attachment list from CImEmailMessage.
       
   364 		iEmailMessage.GetAttachmentsListL(iClientEntry.EntryId(),CImEmailMessage::EAllAttachments, CImEmailMessage::EThisMessageAndEmbeddedMessages);
       
   365 		}
       
   366 	}
       
   367 
       
   368 RPointerArray<CMsvAttachment>& CImAttachmentManager::AttachmentInfoSelection()
       
   369 	{
       
   370 	return iAttachmentInfoList;
       
   371 	}
       
   372 
       
   373 TInt CImAttachmentManager::AttachmentIndexInMessage(TMsvAttachmentId aId)
       
   374 	{
       
   375 	if(!iAttachmentInfoList.Count())	
       
   376 		return KErrNotFound;	
       
   377 	// Traverse the list and get the index of the required attachment
       
   378 	for(TInt i = 0;i < iAttachmentInfoList.Count(); i++)
       
   379 		{
       
   380 		// Id contains unique per-message id.
       
   381 		if(aId == iAttachmentInfoList[i]->Id())
       
   382 			{
       
   383 			// found the entry
       
   384 			return i;
       
   385 			}
       
   386 		}
       
   387 	return KErrNotFound;	
       
   388 	}	
       
   389 	
       
   390 void CImAttachmentManager::AppendAttachmentArrayL(TMsvId aAttachmentId)
       
   391 	{
       
   392 	CMsvEntry* entry=CMsvEntry::NewL(iClientEntry.Session(), aAttachmentId, TMsvSelectionOrdering());
       
   393 	CleanupStack::PushL(entry);
       
   394 
       
   395 	CMsvStore* store = entry->ReadStoreL();
       
   396 	CleanupStack::PushL(store);
       
   397 	
       
   398 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   399 	if(attachmentMgr.AttachmentCount())
       
   400 		{
       
   401 		CMsvAttachment* attachment = attachmentMgr.GetAttachmentInfoL(0);
       
   402 		attachment->SetId(aAttachmentId);	
       
   403 		CleanupStack::PushL(attachment);
       
   404 		iAttachmentInfoList.AppendL(attachment);		
       
   405 		CleanupStack::Pop(attachment);
       
   406 		}
       
   407 	CleanupStack::PopAndDestroy(2,entry);//store, entry	
       
   408 	}
       
   409 
       
   410 void CImAttachmentManager::DeleteAttachmentInArrayL(TMsvId aDeletedAttachmentId)
       
   411 	{
       
   412 	TInt deleteAttachmentIdIndex = AttachmentIndexInMessage(aDeletedAttachmentId);
       
   413 	// leaves if error
       
   414 	User::LeaveIfError(deleteAttachmentIdIndex );
       
   415 	
       
   416 	delete iAttachmentInfoList[deleteAttachmentIdIndex];
       
   417 	iAttachmentInfoList.Remove(deleteAttachmentIdIndex);
       
   418 	}
       
   419 	
       
   420 void CImAttachmentManager::UpdateModifiedAttachmentInListL()
       
   421 	{
       
   422 	CMsvStore* store = iEntry->ReadStoreL();
       
   423 	CleanupStack::PushL(store);
       
   424 	MMsvAttachmentManager& attachmentMgr = store->AttachmentManagerL();
       
   425 	CMsvAttachment* attachmentInfo = attachmentMgr.GetAttachmentInfoL(0);
       
   426 	CleanupStack::PopAndDestroy(store);
       
   427 	
       
   428 	CleanupStack::PushL(attachmentInfo);
       
   429 	attachmentInfo->SetId(iEntry->EntryId());
       
   430 	TInt index = AttachmentIndexInMessage(iEntry->EntryId());
       
   431 	
       
   432 	// Insert the modified attachment at the index which was modified
       
   433 	iAttachmentInfoList.InsertL(attachmentInfo, index);
       
   434 	CleanupStack::Pop(attachmentInfo);
       
   435 	
       
   436 	// remove the previous attachment info from the list
       
   437 	delete iAttachmentInfoList[index+1];
       
   438 	iAttachmentInfoList.Remove(index+1);
       
   439 	}
       
   440 	
       
   441 void CImAttachmentManager::ChangeEntryDetailsL()
       
   442 	{
       
   443 	TInt index = AttachmentIndexInMessage(iEntry->EntryId());
       
   444 	TMsvEntry entry(iEntry->Entry());		
       
   445 	entry.iSize = iAttachmentInfoList[index]->Size();
       
   446 	entry.SetComplete(iAttachmentInfoList[index]->Complete());
       
   447 	
       
   448 	delete iMsvOperation;
       
   449 	iMsvOperation = NULL;
       
   450 	iMsvOperation = iEntry->ChangeL(entry,iStatus);
       
   451 	SetActive();
       
   452 	}
       
   453 
       
   454 void CImAttachmentManager::ModifyAttachmentInfoL()	
       
   455 	{
       
   456 	iStore->CommitL();
       
   457 	delete iStore;
       
   458 	iStore = NULL;
       
   459 	UpdateModifiedAttachmentInListL();
       
   460 	}
       
   461 	
       
   462 TInt CImAttachmentManager::UniqueAttachmentId()
       
   463 	{
       
   464 	// Initialisation of iUniqueId is left to CBase (ie 0) id's start at 1 so pre-increment
       
   465 	return ++iUniqueId;
       
   466 	}
       
   467