meetingrequest/mricalviewer/src/cesmrimapmailfetcher.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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 IMAP mail fetcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "cesmrimapmailfetcher.h"
       
    21 #include "esmricalviewerutils.h"
       
    22 //<cmail>
       
    23 #include "CFSMailClient.h"
       
    24 #include "CFSMailFolder.h"
       
    25 #include "CFSMailMessagePart.h"
       
    26 #include "CFSMailCommon.h"
       
    27 #include "esmricalvieweropcodes.hrh"
       
    28 //</cmail>
       
    29 
       
    30 
       
    31 // Unnamed namespace for local definitions
       
    32 namespace {
       
    33 
       
    34 // Preferred byte count definition
       
    35 const TInt KPreferredByteCount(0);
       
    36 }
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CESMRImapMailFetcherCmd::CESMRImapMailFetcherCmd
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CESMRImapMailFetcherCmd::CESMRImapMailFetcherCmd(
       
    45         CFSMailClient& aMailClient,
       
    46         CCalSession& aCalSession )
       
    47 :   CESMRIcalViewerAsyncCommand(
       
    48         EESMRFetchMailContent,
       
    49         aCalSession ),
       
    50     iMailClient( aMailClient )
       
    51     {
       
    52     FUNC_LOG;
       
    53     iResult.iOpType = OperationType();
       
    54     iResult.iResultCode = KErrNone;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CESMRImapMailFetcherCmd::~CESMRImapMailFetcherCmd
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CESMRImapMailFetcherCmd::~CESMRImapMailFetcherCmd()
       
    62     {
       
    63     FUNC_LOG;
       
    64     CancelCommand();
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CESMRImapMailFetcherCmd::NewL
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CESMRImapMailFetcherCmd* CESMRImapMailFetcherCmd::NewL(
       
    72         CFSMailClient& aMailClient,
       
    73         CCalSession& aCalSession )
       
    74     {
       
    75     FUNC_LOG;
       
    76 
       
    77     CESMRImapMailFetcherCmd* self =
       
    78             new (ELeave) CESMRImapMailFetcherCmd( aMailClient, aCalSession );
       
    79     CleanupStack::PushL(self);
       
    80     self->ConstructL();
       
    81     CleanupStack::Pop(self);
       
    82 
       
    83 
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CESMRImapMailFetcherCmd::ConstructL
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CESMRImapMailFetcherCmd::ConstructL()
       
    92     {
       
    93     FUNC_LOG;
       
    94     //do nothing
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CESMRImapMailFetcherCmd::FetchMailL
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CESMRImapMailFetcherCmd::ExecuteAsyncCommandL()
       
   102     {
       
   103     FUNC_LOG;
       
   104 
       
   105     iResult.iMessage = Message();
       
   106     iResult.iOpType = OperationType();
       
   107     iResult.iResultCode = KErrNone;
       
   108 
       
   109     iMailMessage = Message();
       
   110     iResult.iMessage = iMailMessage;
       
   111     TBool operationStarted( ETrue );
       
   112 
       
   113     TFSPartFetchState messageLoadState( iMailMessage->FetchLoadState() );
       
   114     if ( EFSEmailStructureUnknown == messageLoadState )
       
   115         {
       
   116         // Message structure needs to be fetched first
       
   117         // After structure is fetched, then message parts can be fetched
       
   118         // When structure fetching is completed, part fetching is triggered
       
   119         // automatically
       
   120         FetchMessageStructureL();
       
   121         }
       
   122     else
       
   123         {
       
   124         // Message structure is already known --> Part can be fetched
       
   125         // Wait operation is started if part needs to fetched.
       
   126         operationStarted = FetchMessageContentsL();
       
   127         }
       
   128 
       
   129     if ( !operationStarted )
       
   130         {
       
   131         NotifyCompletion();
       
   132         }
       
   133 
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CESMRImapMailFetcherCmd::CancelAsyncCommand
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void CESMRImapMailFetcherCmd::CancelAsyncCommand()
       
   141     {
       
   142     FUNC_LOG;
       
   143 
       
   144     if ( EIdle != iState )
       
   145         {
       
   146         TRAP_IGNORE( iMailClient.CancelAllL() );
       
   147 
       
   148         iResult.iResultCode = KErrCancel;
       
   149         Observer()->OperationError( iResult );
       
   150         }
       
   151 
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // CESMRImapMailFetcherCmd::RequestResponseL
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CESMRImapMailFetcherCmd::RequestResponseL(
       
   159         TFSProgress aEvent,
       
   160         TInt aRequestId )
       
   161     {
       
   162     FUNC_LOG;
       
   163     TRAPD( err, HandleRequestResponseL(aEvent, aRequestId) );
       
   164 
       
   165     if ( KErrNone != err )
       
   166         {
       
   167         // Error occured --> Cancel operations
       
   168         iResult.iResultCode = err;
       
   169         NotifyCompletion();
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CESMRImapMailFetcherCmd::FetchMessageStructureL
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CESMRImapMailFetcherCmd::FetchMessageStructureL()
       
   178     {
       
   179     FUNC_LOG;
       
   180 
       
   181     TFSMailMsgId currentMailboxId =
       
   182             iMailMessage->GetMailBoxId();
       
   183 
       
   184     TFSMailMsgId currentMessageFolderId =
       
   185             iMailMessage->GetFolderId();
       
   186 
       
   187     CFSMailFolder* currentFolder  =
       
   188             iMailClient.GetFolderByUidL(
       
   189                     currentMailboxId,
       
   190                     currentMessageFolderId );
       
   191 
       
   192     CleanupStack::PushL( currentFolder );
       
   193 
       
   194     iState = EFetchingStructure;
       
   195     RArray<TFSMailMsgId> messageIds;
       
   196     CleanupClosePushL( messageIds );
       
   197     messageIds.Append( iMailMessage->GetMessageId() );
       
   198     iStructureRequestId =
       
   199             currentFolder->FetchMessagesL(
       
   200                     messageIds,
       
   201                     EFSMsgDataStructure,
       
   202                     *this );
       
   203     
       
   204     CleanupStack::PopAndDestroy();//messageIds
       
   205     CleanupStack::PopAndDestroy( currentFolder );
       
   206     
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CESMRImapMailFetcherCmd::FetchMessageContentsL
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 TBool CESMRImapMailFetcherCmd::FetchMessageContentsL()
       
   214     {
       
   215     FUNC_LOG;
       
   216     TBool operationStarted( EFalse );
       
   217 
       
   218     CFSMailMessagePart* calendarPart = ESMRIcalViewerUtils::LocateCalendarPartL( *iMailMessage );
       
   219     if ( !calendarPart )
       
   220         {
       
   221         // message does not contain calendar part at all
       
   222         iResult.iResultCode = KErrNotFound;
       
   223         return operationStarted;
       
   224         }
       
   225     CleanupStack::PushL( calendarPart );
       
   226     
       
   227     TFSPartFetchState partLoadState( calendarPart->FetchLoadState() );
       
   228     if ( EFSFull != partLoadState )
       
   229         {
       
   230         // part is not fetched to device
       
   231         iMessagePartRequestId =
       
   232             calendarPart->FetchMessagePartL(
       
   233                         calendarPart->GetPartId(),
       
   234                         *this,
       
   235                         KPreferredByteCount );
       
   236     
       
   237         iState = EFetchingContents;
       
   238         operationStarted = ETrue;
       
   239         }
       
   240     
       
   241     CleanupStack::PopAndDestroy( calendarPart );
       
   242     calendarPart = NULL;
       
   243     return operationStarted;
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CESMRImapMailFetcherCmd::FetchMessageContentsL
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CESMRImapMailFetcherCmd::HandleRequestResponseL(
       
   251         TFSProgress aEvent,
       
   252         TInt /*aRequestId*/ )
       
   253     {
       
   254     FUNC_LOG;
       
   255 
       
   256     TBool operationStarted( EFalse );
       
   257 
       
   258     if ( /*aRequestId == iStructureRequestId && */
       
   259               EFetchingStructure == iState )
       
   260         {
       
   261         // Fetching message structure
       
   262         switch ( aEvent.iProgressStatus )
       
   263             {
       
   264             case TFSProgress::EFSStatus_RequestComplete:
       
   265                 {
       
   266                 // Starting part fetching
       
   267                 operationStarted = FetchMessageContentsL();
       
   268                 iResult.iResultCode = aEvent.iError;
       
   269                 }
       
   270             break;
       
   271             case TFSProgress::EFSStatus_RequestCancelled:
       
   272                 {
       
   273                 // Error occured during fetch operation
       
   274                 iResult.iResultCode = KErrCancel;
       
   275                 }
       
   276             break;
       
   277             default:
       
   278                 break;
       
   279             }
       
   280         }
       
   281 
       
   282     if ( !operationStarted )
       
   283         {
       
   284         NotifyCompletion();
       
   285         }
       
   286 
       
   287     }
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CESMRImapMailFetcherCmd::NotifyCompletion
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 void CESMRImapMailFetcherCmd::NotifyCompletion()
       
   294     {
       
   295     FUNC_LOG;
       
   296     if ( KErrNone == iResult.iResultCode )
       
   297         {
       
   298         Observer()->OperationCompleted( iResult );
       
   299         }
       
   300     else
       
   301         {
       
   302         Observer()->OperationError( iResult );
       
   303         }
       
   304     }
       
   305 
       
   306 // EOF
       
   307