emailuis/emailui/src/FreestyleEmailUiDownloadManagerModel.cpp
changeset 0 8466d47a6819
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 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:  Implements model for the attachment list view
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDEfS
       
    20 //<cmail>
       
    21 #include "emailtrace.h"
       
    22 #include "cfsmailclient.h"
       
    23 //</cmail>
       
    24 #include <StringLoader.h>
       
    25 #include <FreestyleEmailUi.rsg>
       
    26 
       
    27 // INTERNAL INCLUDES
       
    28 #include "FreestyleEmailUiAppUi.h"
       
    29 #include "FreestyleEmailUiDownloadManagerModel.h"
       
    30 #include "FreestyleEmailDownloadInformationMediator.h"
       
    31 #include "FreestyleEmailUiUtilities.h"
       
    32 
       
    33 
       
    34 //MODEL CLASS CONSTRUCTION
       
    35 CFSEmailUiDownloadManagerModel* CFSEmailUiDownloadManagerModel::NewL( CFreestyleEmailUiAppUi& aAppUi,
       
    36                                                                       MFSEmailUiAttachmentsStatusObserver& aObserver )
       
    37     {
       
    38     FUNC_LOG;
       
    39     CFSEmailUiDownloadManagerModel* self = CFSEmailUiDownloadManagerModel::NewLC( aAppUi, aObserver );
       
    40     CleanupStack::Pop(self);
       
    41     return self;
       
    42     }
       
    43 
       
    44 CFSEmailUiDownloadManagerModel* CFSEmailUiDownloadManagerModel::NewLC( CFreestyleEmailUiAppUi& aAppUi,
       
    45                                                                        MFSEmailUiAttachmentsStatusObserver& aObserver )
       
    46     {
       
    47     FUNC_LOG;
       
    48     CFSEmailUiDownloadManagerModel* self = new (ELeave) CFSEmailUiDownloadManagerModel( aAppUi, aObserver);
       
    49     CleanupStack::PushL(self);
       
    50     self->ConstructL();
       
    51     return self;
       
    52     }
       
    53 
       
    54 void CFSEmailUiDownloadManagerModel::ConstructL()
       
    55     {
       
    56     FUNC_LOG;
       
    57    	CFSEmailUiAttachmentsModelBase::ConstructL();
       
    58 	iMailClient = AppUi().GetMailClient();
       
    59 	if ( iAppUi.DownloadInfoMediator() )
       
    60 		{
       
    61 		iAppUi.DownloadInfoMediator()->AddObserver( this );		
       
    62 		}
       
    63     }
       
    64  
       
    65 CFSEmailUiDownloadManagerModel::CFSEmailUiDownloadManagerModel( CFreestyleEmailUiAppUi& aAppUi,
       
    66                                                                 MFSEmailUiAttachmentsStatusObserver& aObserver ) 
       
    67 	: CFSEmailUiAttachmentsModelBase( aAppUi ), iObserver( aObserver )
       
    68     {
       
    69     FUNC_LOG;
       
    70     }
       
    71 	
       
    72 CFSEmailUiDownloadManagerModel::~CFSEmailUiDownloadManagerModel()
       
    73     {
       
    74     FUNC_LOG;
       
    75     for (TInt i=0; i<iMails.Count(); i++)
       
    76         {
       
    77         iMails[i].mailAttachments.Close();
       
    78         delete iMails[i].mailSubject;
       
    79         }
       
    80     iMails.Close();
       
    81     if ( iAppUi.DownloadInfoMediator() )
       
    82         {
       
    83         iAppUi.DownloadInfoMediator()->StopObserving( this );
       
    84         }
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Returns attachment data for specified tree item, or NULL if not found.
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 TAttachmentData* CFSEmailUiDownloadManagerModel::GetItem( TFsTreeItemId aTreeId )
       
    93     {
       
    94     FUNC_LOG;
       
    95     const TInt mailCount = iMails.Count();
       
    96     for ( TInt i = 0; i < mailCount; i++ )
       
    97         {
       
    98         const TInt attachmentCount = iMails[i].mailAttachments.Count();
       
    99         for ( TInt j = 0; j < attachmentCount; j++)
       
   100             {
       
   101             if ( iMails[i].mailAttachments[j].treeId == aTreeId )
       
   102                 {
       
   103                 return &iMails[i].mailAttachments[j];
       
   104                 }
       
   105             }
       
   106         }
       
   107 
       
   108     return NULL;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Returns attachment data for specified tree item
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 TAttachmentData& CFSEmailUiDownloadManagerModel::GetItemL( TFsTreeItemId aTreeId )
       
   116 	{
       
   117     FUNC_LOG;
       
   118 	TAttachmentData* returnValue = GetItem( aTreeId );
       
   119 	if ( !returnValue )
       
   120 		{
       
   121 		User::Leave( KErrNotFound );
       
   122 		}
       
   123 	return *returnValue;
       
   124 	}
       
   125 
       
   126 TPartData CFSEmailUiDownloadManagerModel::GetMessageL( TFsTreeItemId aTreeId )
       
   127     {
       
   128     FUNC_LOG;
       
   129 	TPartData returnValue;
       
   130 	TBool found = EFalse;
       
   131 	
       
   132 	for ( TInt i=0 ; i<iMails.Count() && !found ; i++ )
       
   133 		{
       
   134 		if ( iMails[i].treeId == aTreeId )
       
   135 		    {
       
   136 		    returnValue = iMails[i].partData;
       
   137 		    found = ETrue;
       
   138 		    }
       
   139 		else
       
   140 		    {
       
   141     		for ( TInt j=0 ; j<iMails[i].mailAttachments.Count() && !found ; j++ )
       
   142     	   		{
       
   143     	   		if ( iMails[i].mailAttachments[j].treeId == aTreeId )
       
   144     	   			{
       
   145     	   			returnValue = iMails[i].partData;
       
   146     	   			found = ETrue;
       
   147     	   			}
       
   148     			}
       
   149 		    }
       
   150 		}
       
   151 	
       
   152 	if ( !found )
       
   153 		{
       
   154 		User::Leave( KErrNotFound );
       
   155 		}
       
   156 	return returnValue;
       
   157     }
       
   158 
       
   159 	
       
   160 TBool CFSEmailUiDownloadManagerModel::RemoveItem( TFsTreeItemId aTreeId )
       
   161 	{
       
   162     FUNC_LOG;
       
   163 	TBool found = EFalse;
       
   164 	for ( TInt i=0 ; !found && i<iMails.Count() ; i++ )
       
   165 		{
       
   166 		for ( TInt j=0 ; !found && j<iMails[i].mailAttachments.Count() ; j++ )
       
   167 			{
       
   168 			if ( iMails[i].mailAttachments[j].treeId == aTreeId )
       
   169 				{
       
   170 			   	iMails[i].mailAttachments.Remove(j);
       
   171 			   	if ( iMails[i].mailAttachments.Count() == 0 )
       
   172 			   		{
       
   173 			   		iMails.Remove(i);			   		
       
   174 			   		}
       
   175 			   	found = ETrue;
       
   176 				}
       
   177 			}
       
   178 	   	}
       
   179 	return found;
       
   180 	}
       
   181 
       
   182 void CFSEmailUiDownloadManagerModel::SetItemIdL(TUint aMailIndex, TUint aAttachmentIndex, TFsTreeItemId aTreeId)
       
   183 	{
       
   184     FUNC_LOG;
       
   185 	if ( iMails.Count()>aMailIndex && iMails[aMailIndex].mailAttachments.Count() > aAttachmentIndex )
       
   186 		{
       
   187 		iMails[aMailIndex].mailAttachments[aAttachmentIndex].treeId = aTreeId;
       
   188 		}
       
   189 	else
       
   190 		{
       
   191 		User::Leave ( KErrNotFound );
       
   192 		}
       
   193 	}
       
   194 
       
   195 const RArray<TMessageData>& CFSEmailUiDownloadManagerModel::GetModel()
       
   196 	{
       
   197 	return iMails;
       
   198 	}
       
   199 
       
   200 TInt CFSEmailUiDownloadManagerModel::AttachmentCount() const
       
   201     {
       
   202     FUNC_LOG;
       
   203     TInt count = 0;
       
   204     
       
   205     for ( TInt i=0 ; i<iMails.Count() ; ++i )
       
   206         {
       
   207         count += iMails[i].mailAttachments.Count();
       
   208         }
       
   209         
       
   210     return count;
       
   211     }
       
   212 	
       
   213 void CFSEmailUiDownloadManagerModel::RequestResponseL( const TFSProgress& aEvent, const TPartData& aPart )
       
   214 	{
       
   215     FUNC_LOG;
       
   216 	TBool attachmentFound = EFalse;
       
   217 	TInt i;
       
   218 	for (i=0; i<iMails.Count(); i++)
       
   219 		{
       
   220 		for (TInt j=0; j<iMails[i].mailAttachments.Count(); j++)
       
   221 			{
       
   222 		   	if ( iMails[i].mailAttachments[j].partData.iMessagePartId == aPart.iMessagePartId )
       
   223 		   		{
       
   224 		   		attachmentFound = ETrue;
       
   225     	   		// Currently, it's not very well defined what should be the values
       
   226     	   		// of each field of TFSProgressStatus in case the download is completed
       
   227     	   		// or cancelled. For now, we make the assumption that these values are
       
   228     	   		// correctly set for status events. In complete event we set the progress
       
   229     	   		// to 100% and in cancel event to 0%, regardless of the counter values.
       
   230     	   		// These may be subjects to change. 
       
   231     	   		if ( aEvent.iError )
       
   232     	   		    {
       
   233     	   		    iMails[i].mailAttachments[j].downloadProgress = 0;
       
   234     	   		    }
       
   235     	   		else
       
   236     	   		    {
       
   237         	   		switch ( aEvent.iProgressStatus )
       
   238         	   		    {
       
   239         	   		    case TFSProgress::EFSStatus_Status:
       
   240         	   		        {
       
   241                 	   		if ( aEvent.iMaxCount > 0 && aEvent.iCounter > 0 )
       
   242                 	   			{
       
   243                 				iMails[i].mailAttachments[j].downloadProgress = 
       
   244                 				    KComplete * aEvent.iCounter / aEvent.iMaxCount;
       
   245                 	   			}
       
   246                 	   		else
       
   247                 	   			{
       
   248                 	   			iMails[i].mailAttachments[j].downloadProgress = KNone;
       
   249                 	   			}
       
   250         	   		        }
       
   251         	   		    break;
       
   252         	   		    
       
   253         	   		    case TFSProgress::EFSStatus_RequestComplete:
       
   254         	   		        {
       
   255         	   		        iMails[i].mailAttachments[j].downloadProgress = KComplete;
       
   256         	   		        }
       
   257         	   		    break;
       
   258         	   		    
       
   259         	   		    case TFSProgress::EFSStatus_RequestCancelled:
       
   260         	   		        {
       
   261         	   		        iMails[i].mailAttachments[j].downloadProgress = KNone;
       
   262         	   		        }
       
   263         	   		    break;
       
   264         	   		    
       
   265         	   		    default:
       
   266         	   		        // do nothing with other events
       
   267         	   		    break;
       
   268         	   		    }
       
   269     	   		    }
       
   270     	   		
       
   271    	   		    iObserver.DownloadStatusChangedL(i);
       
   272 				}
       
   273 			}
       
   274 		}
       
   275 	if ( !attachmentFound )
       
   276 		{
       
   277 		CFSMailMessage* mailMessage = iAppUi.GetMailClient()->GetMessageByUidL( aPart.iMailBoxId, aPart.iFolderId, aPart.iMessageId, EFSMsgDataEnvelope );
       
   278 		CleanupStack::PushL( mailMessage );
       
   279 		CFSMailMessagePart* messagePart = mailMessage->ChildPartL( aPart.iMessagePartId );
       
   280 		CleanupStack::PushL( messagePart );
       
   281 
       
   282     	TFileType fileType = TFsEmailUiUtility::GetFileType( messagePart->AttachmentNameL(),
       
   283     	                                                     messagePart->GetContentType() );
       
   284 		TUint downloadProgress;
       
   285 		if ( aEvent.iMaxCount > 0 )
       
   286 			{
       
   287 			downloadProgress = KComplete * aEvent.iCounter / aEvent.iMaxCount;
       
   288    			}
       
   289    		else
       
   290 			{
       
   291 			downloadProgress = KNone;
       
   292 			}
       
   293 
       
   294 
       
   295 		TAttachmentData attachment = 
       
   296 			{
       
   297 			messagePart->ContentSize(),
       
   298 			downloadProgress,
       
   299 			fileType,
       
   300 			messagePart->AttachmentNameL(),
       
   301 			KFsTreeNoneID,
       
   302 			aPart
       
   303 			};
       
   304 
       
   305 		TBool messageFound = EFalse;
       
   306 		for ( TInt i=0; i<iMails.Count(); i++ )
       
   307 			{
       
   308 			if ( iMails[i].partData.iMessageId  == aPart.iMessageId )
       
   309 				{
       
   310 				// Insert the new attachment as the first entry of the message node. 
       
   311 				iMails[i].mailAttachments.InsertL( attachment, 0 );
       
   312 				iObserver.DownloadStatusChangedL(i);
       
   313 				messageFound = ETrue;
       
   314 				}
       
   315 			}
       
   316 
       
   317 		if ( !messageFound )
       
   318 			{
       
   319 			// If no message node is already present in the list, a new one is created on
       
   320 			// the top of the list.
       
   321 			RArray<TAttachmentData> attachmentData;
       
   322 			attachmentData.Append( attachment );
       
   323 			
       
   324 			HBufC* subjectText = TFsEmailUiUtility::CreateSubjectTextLC( mailMessage );
       
   325 			TMessageData msgData = { subjectText, attachmentData, aPart, KFsTreeNoneID };
       
   326 			iMails.InsertL( msgData, 0 );
       
   327             CleanupStack::Pop( subjectText );
       
   328 			iObserver.DownloadStatusChangedL( 0 );
       
   329 			}
       
   330 		CleanupStack::PopAndDestroy( messagePart );
       
   331 		CleanupStack::PopAndDestroy( mailMessage );
       
   332 		}		
       
   333 	}
       
   334 
       
   335 void CFSEmailUiDownloadManagerModel::SetNodeIdL( TInt aNodeIndex, TFsTreeItemId aTreeItemId )
       
   336 	{
       
   337     FUNC_LOG;
       
   338 	if ( iMails.Count() > aNodeIndex )
       
   339 		{
       
   340 		iMails[aNodeIndex].treeId = aTreeItemId;
       
   341 		}
       
   342 	else
       
   343 		{
       
   344 		User::Leave ( KErrNotFound );
       
   345 		}
       
   346 
       
   347 	}
       
   348 
       
   349 TBool CFSEmailUiDownloadManagerModel::DownloadAllAttachmentsL()
       
   350     {
       
   351     FUNC_LOG;
       
   352     TBool retVal = EFalse;
       
   353 	for (TInt i=0; i<iMails.Count(); i++)
       
   354 		{
       
   355 		for (TInt j=0; j<iMails[i].mailAttachments.Count(); j++)
       
   356 	   		{
       
   357 			TBool downloadStarted = StartDownloadL( iMails[i].mailAttachments[j].treeId );
       
   358 			retVal = downloadStarted || retVal;
       
   359 		   	}
       
   360 		}
       
   361 	return retVal;	
       
   362     }
       
   363 	
       
   364 TBool CFSEmailUiDownloadManagerModel::SaveAllAttachmentsL( const TDesC& aFileName )
       
   365 	{
       
   366     FUNC_LOG;
       
   367 	TBool retVal = EFalse;
       
   368 	TInt savedCount( 0 );
       
   369 	for (TInt i=0; i<iMails.Count(); i++)
       
   370 		{
       
   371 		for (TInt j=0; j<iMails[i].mailAttachments.Count(); j++)
       
   372 	   		{
       
   373 			TBool downloadStarted = SaveAttachmentL( iMails[i].mailAttachments[j].treeId, aFileName, savedCount );
       
   374 			retVal = downloadStarted || retVal;
       
   375 		   	}
       
   376 		}
       
   377 	if ( savedCount )
       
   378 	    {
       
   379 	    // <cmail>
       
   380 	    TFsEmailUiUtility::ShowFilesSavedToFolderNoteL( savedCount );        
       
   381 	    }
       
   382 	return retVal;
       
   383 	}
       
   384 
       
   385 void CFSEmailUiDownloadManagerModel::CancelAllDownloadsL()
       
   386     {
       
   387     FUNC_LOG;
       
   388 	for (TInt i=0; i<iMails.Count(); i++)
       
   389 		{
       
   390 		for (TInt j=0; j<iMails[i].mailAttachments.Count(); j++)
       
   391 	   		{
       
   392 	   		TFSMailMsgId attPartId = iMails[i].mailAttachments[j].partData.iMessagePartId;
       
   393    			if ( iAppUi.DownloadInfoMediator() )
       
   394    				{
       
   395 	   			iAppUi.DownloadInfoMediator()->CancelDownloadL( attPartId );				
       
   396    				}
       
   397 			}
       
   398 		}
       
   399     }
       
   400 
       
   401 TBool CFSEmailUiDownloadManagerModel::IsThereAnyMessageAttachments()
       
   402     {
       
   403     FUNC_LOG;
       
   404     TBool msgFound = EFalse;
       
   405     TInt mailCount = iMails.Count();
       
   406     for ( TInt i = 0 ; i < mailCount && !msgFound ; i++ )
       
   407         {
       
   408         TInt attCount = iMails[i].mailAttachments.Count();
       
   409         for ( TInt j = 0 ; j < attCount && !msgFound ; j++ )
       
   410             {
       
   411             const TAttachmentData& attachment = iMails[i].mailAttachments[j];
       
   412             msgFound = IsMessage( attachment );
       
   413             }
       
   414         }
       
   415     return msgFound;
       
   416     }
       
   417     
       
   418 TBool CFSEmailUiDownloadManagerModel::ClearInvalidItemsL()
       
   419 	{
       
   420     FUNC_LOG;
       
   421 	TBool itemWasRemoved = EFalse;
       
   422 	
       
   423 	// Loop from back to beginning, easier to destroy items.
       
   424 	if ( iMails.Count() )
       
   425 		{
       
   426 		for ( TInt i=iMails.Count()-1; i>=0; i-- )
       
   427 			{
       
   428 			// Remove items from model if they are not valid anymore
       
   429 		    CFSMailMessage* confirmedMsgPtr(0);
       
   430 		    // TRAP KErrNotFound leave with ignore cause null pointer is checked after this
       
   431 		    TRAP_IGNORE( confirmedMsgPtr = iAppUi.GetMailClient()->GetMessageByUidL( iMails[i].partData.iMailBoxId, 
       
   432 				          iMails[i].partData.iFolderId, iMails[i].partData.iMessageId , EFSMsgDataIdOnly ) ); // Check if id is valid
       
   433 			if ( confirmedMsgPtr )
       
   434 				{
       
   435 				delete confirmedMsgPtr; // delete unnecessary object
       
   436 				}		
       
   437 			else
       
   438 				{
       
   439 		        iMails[i].mailAttachments.Close();
       
   440 		        delete iMails[i].mailSubject;
       
   441 				iMails.Remove(i); // Remove
       
   442 				itemWasRemoved = ETrue;
       
   443 				}
       
   444 			}			
       
   445 		}
       
   446 	
       
   447 	return itemWasRemoved;
       
   448 	}
       
   449