meetingrequest/mrcmailremoteattachmentplugin/src/cmrcmailremoteattachmentplugindownloader.cpp
branchRCL_3
changeset 12 4ce476e64c59
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2  * Copyright (c) 2007-2009 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:
       
    15  *
       
    16  */
       
    17 
       
    18 #include "cmrcmailremoteattachmentplugindownloader.h"
       
    19 #include "mcalremoteattachmentoperationobserver.h"
       
    20 #include "ccalremoteattachment.h"
       
    21 #include "esmrhelper.h"
       
    22 #include "cfsmailclient.h"
       
    23 #include "cfsmailmessage.h"
       
    24 
       
    25 #include <coemain.h>
       
    26 
       
    27 #include "emailtrace.h"
       
    28 
       
    29 // Unnamed namespace for local definitions
       
    30 namespace { // codescanner::namespace
       
    31 
       
    32 // Preferred byte count definition
       
    33 const TInt KPreferredByteCount(0);
       
    34 
       
    35 // Percentage for progress calculation
       
    36 const TInt KPercentage( 100 );
       
    37 
       
    38 // Literal for temporary path
       
    39 _LIT( KTempPath, "temp\\" );
       
    40 
       
    41 }
       
    42 
       
    43 // ======== MEMBER FUNCTIONS ========
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader(
       
    50         CFSMailClient& aMailClient,
       
    51         MCalRemoteAttachmentOperationObserver& aObserver )
       
    52 :   CActive( EPriorityStandard ),
       
    53     iMailClient( aMailClient ),
       
    54     iObserver( aObserver )
       
    55     {
       
    56     CActiveScheduler::Add( this );
       
    57     FUNC_LOG;
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CMRCmailRemoteAttachmentDownloader::~CMRCmailRemoteAttachmentDownloader()
       
    65     {
       
    66     FUNC_LOG;
       
    67 
       
    68     if( IsActive() )
       
    69     	{
       
    70 		Cancel();
       
    71     	}
       
    72     else
       
    73     	{
       
    74 		DoCancel();
       
    75     	}
       
    76     delete iMailMessage;
       
    77     delete iUri;
       
    78     iAttachmentFile.Close();
       
    79     delete iRemoteAttachment;
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CMRCmailRemoteAttachmentDownloader*
       
    87         CMRCmailRemoteAttachmentDownloader::NewL(
       
    88                 CFSMailClient& aMailClient,
       
    89                 MCalRemoteAttachmentOperationObserver& aObserver,
       
    90                 const TDesC& aUri )
       
    91     {
       
    92     FUNC_LOG;
       
    93     CMRCmailRemoteAttachmentDownloader* self =
       
    94             new (ELeave) CMRCmailRemoteAttachmentDownloader(
       
    95                     aMailClient,
       
    96                     aObserver );
       
    97 
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL( aUri );
       
   100     CleanupStack::Pop( self );
       
   101     return self;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 void CMRCmailRemoteAttachmentDownloader::ConstructL(
       
   109         const TDesC& aUri )
       
   110     {
       
   111     FUNC_LOG;
       
   112     iUri = aUri.AllocL();
       
   113 
       
   114     TInt error = iMailIdParser.Parse( *iUri );
       
   115     User::LeaveIfError( error );
       
   116 
       
   117     // Fetch message structure and construct attachment information
       
   118     iMailMessage = iMailClient.GetMessageByUidL(
       
   119             iMailIdParser.iMailboxUid,
       
   120             iMailIdParser.iFolderUid,
       
   121             iMailIdParser.iMessageUid,
       
   122             EFSMsgDataStructure );
       
   123 
       
   124     CFSMailMessagePart* attachment =
       
   125         iMailMessage->ChildPartL( iMailIdParser.iMessagepartUid );
       
   126     CleanupStack::PushL( attachment );
       
   127 
       
   128     TPtrC attachmentName( attachment->AttachmentNameL() );
       
   129     TInt attachmentSize( attachment->ContentSize() );
       
   130 
       
   131     MCalRemoteAttachment::TCalRemoteAttachmentState attachmentState(
       
   132             MCalRemoteAttachment::EAttachmentStateNotDownloaded );
       
   133 
       
   134     if ( EFSFull == attachment->FetchLoadState() )
       
   135         {
       
   136         attachmentState = MCalRemoteAttachment::EAttachmentStateDownloaded;
       
   137         }
       
   138 
       
   139     iRemoteAttachment = CCalRemoteAttachment::NewL(
       
   140                                 aUri,
       
   141                                 attachmentName,
       
   142                                 attachmentSize,
       
   143                                 attachmentState );
       
   144 
       
   145     CleanupStack::PopAndDestroy( attachment );
       
   146 
       
   147     // Trigger asynchronous request
       
   148     IssueRequest();
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CMRCmailRemoteAttachmentDownloader::DoCancel
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void CMRCmailRemoteAttachmentDownloader::DoCancel()
       
   156     {
       
   157     FUNC_LOG;
       
   158 
       
   159     TRAP_IGNORE( DoCancelL() );
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CMRCmailRemoteAttachmentDownloader::RunL
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CMRCmailRemoteAttachmentDownloader::RunL()
       
   167     {
       
   168     FUNC_LOG;
       
   169 
       
   170     // We first check whether attachment is already downloaded to device or not
       
   171     CFSMailMessagePart* attachment =
       
   172         iMailMessage->ChildPartL( iMailIdParser.iMessagepartUid );
       
   173     CleanupStack::PushL( attachment );
       
   174 
       
   175     if ( EFSFull == attachment->FetchLoadState() )
       
   176         {
       
   177         // Attachment is fecthed to device --> Trigger completion
       
   178         NotifyCompletionL( *attachment );
       
   179         }
       
   180     else
       
   181         {
       
   182         // Attachment is not fetched --> Start fetching attachment
       
   183         iFSMailOperationId =
       
   184                 attachment->FetchMessagePartL(
       
   185                             iMailIdParser.iMessagepartUid,
       
   186                             *this,
       
   187                             KPreferredByteCount );
       
   188         }
       
   189 
       
   190     CleanupStack::PopAndDestroy( attachment );
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 // CMRCmailRemoteAttachmentDownloader::RunError
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 TInt CMRCmailRemoteAttachmentDownloader::RunError( TInt aError )
       
   198     {
       
   199     FUNC_LOG;
       
   200 
       
   201     NotifyError( aError );
       
   202 
       
   203     return KErrNone;
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 TInt CMRCmailRemoteAttachmentDownloader::Progress() const
       
   211     {
       
   212     FUNC_LOG;
       
   213 
       
   214     TInt progress( 0 );
       
   215     TRAP_IGNORE( progress = ProgressL() );
       
   216     return progress;
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 const MCalRemoteAttachment&
       
   224         CMRCmailRemoteAttachmentDownloader::AttachmentInformation() const
       
   225     {
       
   226     FUNC_LOG;
       
   227     return *iRemoteAttachment;
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CMRCmailRemoteAttachmentDownloader::RequestResponseL(
       
   235         TFSProgress aEvent,
       
   236         TInt aRequestId )
       
   237     {
       
   238     FUNC_LOG;
       
   239 
       
   240     // Make sure that correct notification is received from mail framework
       
   241     ASSERT( aRequestId == iFSMailOperationId );
       
   242 
       
   243     if ( aRequestId == iFSMailOperationId )
       
   244         {
       
   245         // Fetching message structure
       
   246         switch ( aEvent.iProgressStatus )
       
   247             {
       
   248             case TFSProgress::EFSStatus_RequestComplete:
       
   249                 {
       
   250                 // Fetch attachment part and notify completion
       
   251                 CFSMailMessagePart* attachment =
       
   252                     iMailMessage->ChildPartL( iMailIdParser.iMessagepartUid );
       
   253                 CleanupStack::PushL( attachment );
       
   254 
       
   255                 NotifyCompletionL( *attachment );
       
   256 
       
   257                 CleanupStack::PopAndDestroy( attachment );
       
   258                 }
       
   259                 break;
       
   260 
       
   261             case TFSProgress::EFSStatus_Status:
       
   262                 {
       
   263                 // Notify progress about downloading
       
   264                 TInt progress = ( KPercentage * aEvent.iCounter ) / aEvent.iMaxCount;
       
   265                 iObserver.Progress( this, progress );
       
   266                 }
       
   267                 break;
       
   268 
       
   269             case TFSProgress::EFSStatus_RequestCancelled:
       
   270                 {
       
   271                 // Error occured during fetch operation
       
   272                 NotifyError( KErrCancel );
       
   273                 }
       
   274                 break;
       
   275             default:
       
   276                 break;
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CMRCmailRemoteAttachmentDownloader::IssueRequest()
       
   286     {
       
   287     FUNC_LOG;
       
   288 
       
   289     SetActive();
       
   290     TRequestStatus* status = &iStatus;
       
   291     User::RequestComplete( status, KErrNone );
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // CMRCmailRemoteAttachmentDownloader::CMRCmailRemoteAttachmentDownloader
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 void CMRCmailRemoteAttachmentDownloader::NotifyCompletionL(
       
   299         CFSMailMessagePart& aAttachment )
       
   300     {
       
   301     FUNC_LOG;
       
   302 
       
   303     // Make sure that file server can share files between processes
       
   304     RFs& fs = CCoeEnv::Static()->FsSession();
       
   305     fs.ShareProtected();
       
   306 
       
   307     // Construct filename for attachment
       
   308     TFileName fileName( KTempPath );
       
   309     fileName.Append( iRemoteAttachment->AttachmentLabel() );
       
   310     User::LeaveIfError(
       
   311             ESMRHelper::CreateAndAppendOthersDirToFileName( fileName ) );
       
   312 
       
   313     // Copy attachment from mail storage to file system
       
   314     aAttachment.CopyContentFileL( fileName );
       
   315 
       
   316     // Open file handle to attachment
       
   317     User::LeaveIfError( iAttachmentFile.Open(
       
   318                         fs,
       
   319                         fileName,
       
   320                         EFileRead | EFileShareReadersOnly ) );
       
   321 
       
   322     // Notify observer about completion
       
   323     iRemoteAttachment->SetAttachmentState(
       
   324             MCalRemoteAttachment::EAttachmentStateDownloaded );
       
   325 
       
   326     iObserver.OperationCompleted( this, iAttachmentFile );
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CMRCmailRemoteAttachmentDownloader::NotifyError
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CMRCmailRemoteAttachmentDownloader::NotifyError( TInt aError )
       
   334     {
       
   335     FUNC_LOG;
       
   336     iObserver.OperationError( this, aError );
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // CMRCmailRemoteAttachmentDownloader::ProgressL
       
   341 // ---------------------------------------------------------------------------
       
   342 //
       
   343 TInt CMRCmailRemoteAttachmentDownloader::ProgressL() const
       
   344     {
       
   345     FUNC_LOG;
       
   346 
       
   347     TInt progress( 0 );
       
   348 
       
   349     CFSMailMessagePart* attachment =
       
   350         iMailMessage->ChildPartL( iMailIdParser.iMessagepartUid );
       
   351     CleanupStack::PushL( attachment );
       
   352 
       
   353     TInt fetchedContent( attachment->FetchedContentSize() );
       
   354     TInt contentSize( attachment->ContentSize() );
       
   355     progress = ( KPercentage * fetchedContent ) / contentSize;
       
   356 
       
   357     CleanupStack::PopAndDestroy( attachment );
       
   358 
       
   359     return progress;
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CMRCmailRemoteAttachmentDownloader::DoCancelL
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 void CMRCmailRemoteAttachmentDownloader::DoCancelL()
       
   367     {
       
   368     FUNC_LOG;
       
   369 
       
   370     CFSMailMessagePart* attachment =
       
   371         iMailMessage->ChildPartL( iMailIdParser.iMessagepartUid );
       
   372     CleanupStack::PushL( attachment );
       
   373 
       
   374     if ( EFSFull != attachment->FetchLoadState() )
       
   375         {
       
   376         iMailClient.CancelL( iFSMailOperationId );
       
   377         }
       
   378 
       
   379     CleanupStack::PopAndDestroy( attachment );
       
   380     }
       
   381 
       
   382 // EOF