meetingrequest/mricalviewer/src/cmrattachmentinfofetcher.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:  Implementation for MR attachment fetcher
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "cmrattachmentinfofetcher.h"
       
    20 #include "esmricalviewerutils.h"
       
    21 
       
    22 #include "cfsmailclient.h"
       
    23 #include "cfsmailfolder.h"
       
    24 #include "cfsmailmessagepart.h"
       
    25 #include "cfsmailcommon.h"
       
    26 #include "esmricalvieweropcodes.hrh"
       
    27 
       
    28 #include <ct/rcpointerarray.h>
       
    29 #include <calentry.h>
       
    30 #include <calattachment.h>
       
    31 
       
    32 // Unnamed namespace for local definitions
       
    33 namespace { // codescanner::namespace
       
    34 
       
    35 // Maximum URI length 
       
    36 const TInt KMaxUriLength( 256 );
       
    37 
       
    38 // CMail attachment URI
       
    39 _LIT8( KCMailUri, "cmail://");
       
    40 
       
    41 // Literal for URI UID format
       
    42 _LIT8( KUriUIDFormat, "%d.%d/");
       
    43 
       
    44 /**
       
    45  * Appends attachment information to URI.
       
    46  * @param aUri Reference to URI
       
    47  * @param aId Id to be added
       
    48  */
       
    49 void AppendMailIdToUri( 
       
    50         TDes8& aUri,
       
    51         TFSMailMsgId aId )
       
    52     {    
       
    53     aUri.AppendFormat( KUriUIDFormat,
       
    54                        aId.PluginId().iUid,
       
    55                        aId.Id() );
       
    56     }
       
    57 }
       
    58 
       
    59 // ======== MEMBER FUNCTIONS ========
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CMRAttachmentInfoFetcher::CMRAttachmentInfoFetcher
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CMRAttachmentInfoFetcher::CMRAttachmentInfoFetcher(
       
    66         CFSMailClient& aMailClient,
       
    67         CCalSession& aCalSession,
       
    68         CCalEntry& aCalEntry )
       
    69 :   CESMRIcalViewerAsyncCommand(
       
    70         EESMRFetchAttachmentInfo,
       
    71         aCalSession ),
       
    72     iMailClient( aMailClient ),
       
    73     iCalEntry( aCalEntry )
       
    74     {
       
    75     FUNC_LOG;
       
    76     iResult.iOpType = OperationType();
       
    77     iResult.iResultCode = KErrNone;
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CMRAttachmentInfoFetcher::~CMRAttachmentInfoFetcher
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 CMRAttachmentInfoFetcher::~CMRAttachmentInfoFetcher()
       
    85     {
       
    86     FUNC_LOG;
       
    87     CancelCommand();
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CMRAttachmentInfoFetcher::NewL
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CMRAttachmentInfoFetcher* CMRAttachmentInfoFetcher::NewL(
       
    95         CFSMailClient& aMailClient,
       
    96         CCalSession& aCalSession,
       
    97         CCalEntry& aCalEntry )
       
    98     {
       
    99     FUNC_LOG;
       
   100 
       
   101     CMRAttachmentInfoFetcher* self =
       
   102             new (ELeave) CMRAttachmentInfoFetcher( 
       
   103                     aMailClient, aCalSession, aCalEntry );
       
   104     CleanupStack::PushL(self);
       
   105     self->ConstructL();
       
   106     CleanupStack::Pop(self);
       
   107 
       
   108     return self;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CMRAttachmentInfoFetcher::ConstructL
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CMRAttachmentInfoFetcher::ConstructL()
       
   116     {
       
   117     FUNC_LOG;
       
   118     //do nothing
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // CMRAttachmentInfoFetcher::FetchMailL
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void CMRAttachmentInfoFetcher::ExecuteAsyncCommandL()
       
   126     {
       
   127     FUNC_LOG;
       
   128 
       
   129     iResult.iMessage = Message();
       
   130     iResult.iOpType = OperationType();
       
   131     iResult.iResultCode = KErrNone;
       
   132 
       
   133     iMailMessage = Message();
       
   134     iResult.iMessage = iMailMessage;
       
   135     TBool operationStarted( ETrue );
       
   136 
       
   137     TFSPartFetchState messageLoadState( iMailMessage->FetchLoadState() );
       
   138     if( !iMailMessage->IsFlagSet( EFSMsgFlag_Attachments ) )
       
   139         {
       
   140         operationStarted = EFalse;
       
   141         }
       
   142     else if ( EFSEmailStructureUnknown == messageLoadState )
       
   143         {
       
   144         // Message structure needs to be fetched first
       
   145         // After structure is fetched, then message parts can be fetched
       
   146         // When structure fetching is completed, part fetching is triggered
       
   147         // automatically
       
   148         FetchMessageStructureL();
       
   149         }
       
   150     else
       
   151         {
       
   152         // Message structure is already known --> Part can be fetched
       
   153         // Wait operation is started if part needs to fetched.
       
   154         operationStarted = ConstructAttachmentInformationL();
       
   155         }
       
   156 
       
   157     if ( !operationStarted )
       
   158         {
       
   159         NotifyCompletion();
       
   160         }
       
   161 
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CMRAttachmentInfoFetcher::CancelAsyncCommand
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void CMRAttachmentInfoFetcher::CancelAsyncCommand()
       
   169     {
       
   170     FUNC_LOG;
       
   171 
       
   172     if ( EIdle != iState )
       
   173         {
       
   174         TRAP_IGNORE( iMailClient.CancelAllL() );
       
   175 
       
   176         iResult.iResultCode = KErrCancel;
       
   177         Observer()->OperationError( iResult );
       
   178         }
       
   179 
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CMRAttachmentInfoFetcher::RequestResponseL
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void CMRAttachmentInfoFetcher::RequestResponseL(
       
   187         TFSProgress aEvent,
       
   188         TInt aRequestId )
       
   189     {
       
   190     FUNC_LOG;
       
   191     TRAPD( err, HandleRequestResponseL(aEvent, aRequestId) );
       
   192 
       
   193     if ( KErrNone != err )
       
   194         {
       
   195         // Error occured --> Cancel operations
       
   196         iResult.iResultCode = err;
       
   197         NotifyCompletion();
       
   198         }
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CMRAttachmentInfoFetcher::FetchMessageStructureL
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void CMRAttachmentInfoFetcher::FetchMessageStructureL()
       
   206     {
       
   207     FUNC_LOG;
       
   208 
       
   209     TFSMailMsgId currentMailboxId =
       
   210             iMailMessage->GetMailBoxId();
       
   211 
       
   212     TFSMailMsgId currentMessageFolderId =
       
   213             iMailMessage->GetFolderId();
       
   214 
       
   215     CFSMailFolder* currentFolder  =
       
   216             iMailClient.GetFolderByUidL(
       
   217                     currentMailboxId,
       
   218                     currentMessageFolderId );
       
   219 
       
   220     CleanupStack::PushL( currentFolder );
       
   221 
       
   222     iState = EFetchingStructure;
       
   223     RArray<TFSMailMsgId> messageIds;
       
   224     CleanupClosePushL( messageIds );
       
   225     messageIds.Append( iMailMessage->GetMessageId() );
       
   226     iStructureRequestId =
       
   227             currentFolder->FetchMessagesL(
       
   228                     messageIds,
       
   229                     EFSMsgDataStructure,
       
   230                     *this );
       
   231     
       
   232     CleanupStack::PopAndDestroy();//messageIds
       
   233     CleanupStack::PopAndDestroy( currentFolder );
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CMRAttachmentInfoFetcher::ConstructAttachmentInformationL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 TBool CMRAttachmentInfoFetcher::ConstructAttachmentInformationL()
       
   241     {
       
   242     FUNC_LOG;
       
   243     
       
   244     TBool operationStarted( EFalse );
       
   245     
       
   246     TFSMailMsgId calpartId;
       
   247     calpartId.SetNullId();
       
   248     
       
   249     CFSMailMessage* msg = Message();
       
   250 
       
   251     // Fetch own pointer email message, because message structure is
       
   252     // not necessarily up to date
       
   253     CFSMailMessage* message =
       
   254             iMailClient.GetMessageByUidL(
       
   255                     msg->GetMailBoxId(),
       
   256                     msg->GetFolderId(),
       
   257                     msg->GetMessageId(),
       
   258                     EFSMsgDataStructure );
       
   259     CleanupStack::PushL( message );    
       
   260     
       
   261     CFSMailMessagePart* calendarPart = 
       
   262             ESMRIcalViewerUtils::LocateCalendarPartL( *message );
       
   263     
       
   264     if ( !calendarPart && !message->IsMRInfoSet() )
       
   265         {
       
   266         // message does not contain calendar part at all
       
   267         iResult.iResultCode = KErrNotFound;
       
   268         User::Leave( KErrNotFound );
       
   269         }    
       
   270     
       
   271     CleanupStack::PushL( calendarPart );
       
   272     
       
   273     if ( calendarPart )
       
   274         {
       
   275         calpartId = calendarPart->GetPartId();
       
   276         }
       
   277     
       
   278     RCPointerArray<CFSMailMessagePart> attachments;
       
   279     CleanupClosePushL( attachments );
       
   280 
       
   281     message->AttachmentListL( attachments );
       
   282     
       
   283     TInt attachmentCount( attachments.Count() );
       
   284     for( TInt i(0); i < attachmentCount; i++ )
       
   285         {        
       
   286         TInt contentSize( attachments[i]->ContentSize() );
       
   287         TPtrC attachmentName( attachments[i]->AttachmentNameL() );
       
   288 
       
   289         if ( contentSize >= 0 && attachmentName.Length() &&
       
   290              calpartId !=  attachments[i]->GetPartId() )
       
   291             {
       
   292             HBufC8* uriBuf = HBufC8::NewLC( KMaxUriLength );
       
   293             TPtr8 uri( uriBuf->Des() );
       
   294             
       
   295             uri.Append( KCMailUri );
       
   296             
       
   297             // Setting attachment URI
       
   298             AppendMailIdToUri( uri, message->GetMailBoxId() );
       
   299             AppendMailIdToUri( uri, message->GetFolderId() );
       
   300             AppendMailIdToUri( uri, message->GetMessageId() );
       
   301             AppendMailIdToUri( uri, attachments[i]->GetPartId() );
       
   302       
       
   303             CCalAttachment* attachment = CCalAttachment::NewUriL( uri );
       
   304             CleanupStack::PushL( attachment );
       
   305                         
       
   306             // Setting attachment label
       
   307             attachment->SetLabelL( 
       
   308                     attachments[i]->AttachmentNameL() );
       
   309             
       
   310             iCalEntry.AddAttachmentL( *attachment );
       
   311             CleanupStack::Pop( attachment );
       
   312             CleanupStack::PopAndDestroy( uriBuf );
       
   313             }
       
   314         }
       
   315     
       
   316     CleanupStack::PopAndDestroy( &attachments );
       
   317     CleanupStack::PopAndDestroy( calendarPart );
       
   318     CleanupStack::PopAndDestroy( message );
       
   319     
       
   320     return operationStarted;
       
   321     }
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CMRAttachmentInfoFetcher::FetchMessageContentsL
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 void CMRAttachmentInfoFetcher::HandleRequestResponseL(
       
   328         TFSProgress aEvent,
       
   329         TInt /*aRequestId*/ )
       
   330     {
       
   331     FUNC_LOG;
       
   332 
       
   333     TBool operationStarted( EFalse );
       
   334 
       
   335     if ( /*aRequestId == iStructureRequestId && */
       
   336               EFetchingStructure == iState )
       
   337         {
       
   338         // Fetching message structure
       
   339         switch ( aEvent.iProgressStatus )
       
   340             {
       
   341             case TFSProgress::EFSStatus_RequestComplete:
       
   342                 {
       
   343                 // Starting part fetching
       
   344                 operationStarted = ConstructAttachmentInformationL();
       
   345                 iResult.iResultCode = aEvent.iError;
       
   346                 }
       
   347             break;
       
   348             case TFSProgress::EFSStatus_RequestCancelled:
       
   349                 {
       
   350                 // Error occured during fetch operation
       
   351                 iResult.iResultCode = KErrCancel;
       
   352                 }
       
   353             break;
       
   354             default:
       
   355                 break;
       
   356             }
       
   357         }
       
   358 
       
   359     if ( !operationStarted )
       
   360         {
       
   361         NotifyCompletion();
       
   362         }
       
   363 
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CMRAttachmentInfoFetcher::NotifyCompletion
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 void CMRAttachmentInfoFetcher::NotifyCompletion()
       
   371     {
       
   372     FUNC_LOG;
       
   373     if ( KErrNone == iResult.iResultCode )
       
   374         {
       
   375         Observer()->OperationCompleted( iResult );
       
   376         }
       
   377     else
       
   378         {
       
   379         Observer()->OperationError( iResult );
       
   380         }
       
   381     }
       
   382 
       
   383 // EOF