email/imum/Mtms/Src/Imap4ConnectedOp.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2006 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 *       Base class for operations requiring connection
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <eikenv.h>
       
    21 #include <eikrutil.h>
       
    22 #include <imapcmds.h>
       
    23 
       
    24 #include "Imap4ConnectedOp.h"
       
    25 #include "EmailMsgSizerOperation.h"
       
    26 #include "EmailUtils.H"
       
    27 #include "ImumDisconnectOperation.h"
       
    28 #include "IMAPPRGR.H"
       
    29 #include "ImumMtmLogging.h"
       
    30 
       
    31 
       
    32 #include "ImumPanic.h"
       
    33 
       
    34 // Constants and defines
       
    35 const TInt KConnectedOpPriority = CActive::EPriorityStandard;
       
    36 const TInt KConnectedOpConnectPriority = CActive::EPriorityStandard;
       
    37 
       
    38 CImap4ConnectedOp::~CImap4ConnectedOp()
       
    39     {
       
    40     IMUM_CONTEXT( CImap4ConnectedOp::~CImap4ConnectedOp, 0, KImumMtmLog );
       
    41     IMUM_IN();
       
    42     
       
    43     delete iConnectedOpErrorProgress;
       
    44     IMUM_OUT();
       
    45     }
       
    46 
       
    47 const TDesC8& CImap4ConnectedOp::ProgressL()
       
    48     {
       
    49     IMUM_CONTEXT( CImap4ConnectedOp::ProgressL, 0, KImumMtmLog );
       
    50     IMUM_IN();
       
    51     
       
    52     if(iConnectedOpErrorProgress && (iState == EStateIdle))
       
    53         {
       
    54         // Completed, but with an error during connected operation.
       
    55         return *iConnectedOpErrorProgress;
       
    56         }
       
    57     IMUM_OUT();
       
    58     return CImumOnlineOperation::ProgressL();
       
    59     }
       
    60 
       
    61 const TDesC8& CImap4ConnectedOp::GetErrorProgressL(TInt aError)
       
    62     {
       
    63     IMUM_CONTEXT( CImap4ConnectedOp::GetErrorProgressL, 0, KImumMtmLog );
       
    64     IMUM_IN();
       
    65     
       
    66     // Called if DoRunL leaves.
       
    67     switch(iState)
       
    68         {
       
    69         case EStateConnecting:
       
    70         case EStateDisconnecting:
       
    71             if(!iProgressBuf().iGenericProgress.iErrorCode)
       
    72                 {
       
    73                 TImap4CompoundProgress& prog = iProgressBuf();
       
    74                 prog.iGenericProgress.iState =
       
    75                     (iState == EStateConnecting) ?
       
    76                     (TImap4GenericProgress::EConnecting) :
       
    77                     (TImap4GenericProgress::EDisconnecting);
       
    78                 prog.iGenericProgress.iErrorCode = aError;
       
    79                 }
       
    80             IMUM_OUT();
       
    81             return iProgressBuf;
       
    82 
       
    83         case EStateDoingOp:
       
    84         case EStateIdle:
       
    85         default:
       
    86             IMUM_OUT();
       
    87             // Call pure virtual function to get progress for the connected operation.
       
    88             return ConnectedOpErrorProgressL(aError);
       
    89 
       
    90         }
       
    91     }
       
    92 
       
    93 CImap4ConnectedOp::CImap4ConnectedOp(
       
    94     CImumInternalApi& aMailboxApi,
       
    95     TRequestStatus& aStatus,
       
    96     MMsvProgressReporter& aReporter,
       
    97     TMsvId aServiceId )
       
    98     :
       
    99     CImumDiskSpaceObserverOperation(aMailboxApi, KConnectedOpPriority, aStatus, aReporter),
       
   100     iConnectionCompletionState(CImapConnectionOp::EWithSyncCompleteAfterFullSync)
       
   101     {
       
   102     IMUM_CONTEXT( CImap4ConnectedOp::CImap4ConnectedOp, 0, KImumMtmLog );
       
   103     IMUM_IN();
       
   104     
       
   105     iService = aServiceId;
       
   106     IMUM_OUT();
       
   107     }
       
   108 
       
   109 void CImap4ConnectedOp::ConstructL( TInt /* aFuncId */ )
       
   110     {
       
   111     IMUM_CONTEXT( CImap4ConnectedOp::ConstructL, 0, KImumMtmLog );
       
   112     IMUM_IN();
       
   113     
       
   114     BaseConstructL(KUidMsgTypeIMAP4);
       
   115 
       
   116     StartL();
       
   117     IMUM_OUT();
       
   118     }
       
   119 
       
   120 void CImap4ConnectedOp::StartL()
       
   121     {
       
   122     IMUM_CONTEXT( CImap4ConnectedOp::StartL, 0, KImumMtmLog );
       
   123     IMUM_IN();
       
   124     
       
   125     CMsvEntry* centry = iMsvSession.GetEntryL(iService);
       
   126     iDisconnect = !(centry->Entry().Connected());
       
   127     // Only disconnect afterwards if not currently connected.
       
   128     delete centry;
       
   129 
       
   130     if(iDisconnect)
       
   131         {
       
   132         iState = EStateConnecting;
       
   133         DoConnectL();
       
   134         }
       
   135     else
       
   136         {
       
   137         // Already connected, get on with it.
       
   138         iState = EStateDoingOp;
       
   139         DoConnectedOpL();
       
   140         }
       
   141     IMUM_OUT();
       
   142     }
       
   143 
       
   144 void CImap4ConnectedOp::RunL()
       
   145     {
       
   146     IMUM_CONTEXT( CImap4ConnectedOp::RunL, 0, KImumMtmLog );
       
   147     IMUM_IN();
       
   148     
       
   149     const TInt previousError = iError;
       
   150     TRAP(iError, DoRunL());	// CSI: 86 # Needed here to properly handle error situations
       
   151     if(iError != KErrNone)
       
   152         {
       
   153         // Need to ensure we disconnect if we have connected.
       
   154         if( (iError == KErrNoMemory) || (iState == EStateDisconnecting) )
       
   155             {
       
   156             // OOM and/or failed to create disconnect operation. Call StopService()
       
   157             iMsvSession.StopService(iService);
       
   158             // Ignore return, nothing we can do.
       
   159             }
       
   160         else if( (iState == EStateDoingOp) && (iDisconnect) )
       
   161             {
       
   162             // Failed to start connected operation. Need to disconnect.
       
   163             SetActive();
       
   164             CompleteThis();
       
   165             IMUM_OUT();
       
   166             // Next DoRunL() will attempt to disconnect.
       
   167             return;
       
   168             }
       
   169         CompleteObserver();
       
   170         }
       
   171     else if(previousError != KErrNone)
       
   172         {
       
   173         iError = previousError;
       
   174         }
       
   175     IMUM_OUT();
       
   176     }
       
   177     
       
   178 void CImap4ConnectedOp::DoCancel()
       
   179     {
       
   180     IMUM_CONTEXT( CImap4ConnectedOp::DoCancel, 0, KImumMtmLog );
       
   181     IMUM_IN();
       
   182     
       
   183     CImumOnlineOperation::DoCancel();
       
   184     if ( iState == EStateConnecting )
       
   185         {
       
   186         // Cancelled while doing connected op. Need to disconnect.
       
   187         iMsvSession.StopService( iService );
       
   188         // Ignore return, nothing we can do.
       
   189         }
       
   190     IMUM_OUT();
       
   191     }
       
   192 
       
   193 void CImap4ConnectedOp::DoRunL()
       
   194     {
       
   195     IMUM_CONTEXT( CImap4ConnectedOp::DoRunL, 0, KImumMtmLog );
       
   196     IMUM_IN();
       
   197     
       
   198     switch(iState)
       
   199         {
       
   200         case EStateConnecting:
       
   201             // Connection completed.
       
   202             if(GetOperationCompletionCodeL() != KErrNone)
       
   203                 {
       
   204                 iState = EStateIdle;
       
   205                 CompleteObserver();
       
   206                 }
       
   207             else
       
   208                 {
       
   209                 iState = EStateDoingOp;
       
   210                 DoConnectedOpL();
       
   211                 }
       
   212             break;
       
   213         case EStateDoingOp:
       
   214             // Connected operation completed.
       
   215             if(GetOperationCompletionCodeL() != KErrNone)
       
   216                 {
       
   217                 // Store the failure progress.
       
   218                 __ASSERT_DEBUG(!iConnectedOpErrorProgress,
       
   219                     User::Panic(KImumMtmUiPanic,EPanicAlreadyHaveErrProg));
       
   220                 iConnectedOpErrorProgress = iOperation->ProgressL().AllocL();
       
   221                 }
       
   222 
       
   223             /* Background Handling */
       
   224             MsvEmailMtmUiUtils::CallNewMessagesL( iService );
       
   225             iState = EStateDisconnecting;
       
   226             DoDisconnectL();
       
   227             break;
       
   228         case EStateDisconnecting:
       
   229             // Disonnection completed.
       
   230             iState = EStateIdle;
       
   231             CompleteObserver();
       
   232             break;
       
   233         case EStateIdle:
       
   234         default:
       
   235             break;
       
   236         }
       
   237     if(iOperation)
       
   238         {
       
   239         iMtm = iOperation->Mtm();
       
   240         }
       
   241     IMUM_OUT();
       
   242     }
       
   243 
       
   244 void CImap4ConnectedOp::DoConnectL()
       
   245     {
       
   246     IMUM_CONTEXT( CImap4ConnectedOp::DoConnectL, 0, KImumMtmLog );
       
   247     IMUM_IN();
       
   248     
       
   249     __ASSERT_DEBUG(!iOperation, User::Panic(KImumMtmUiPanic,EPanicOpAlreadySet));
       
   250     delete iOperation;
       
   251     iOperation = NULL;
       
   252     iStatus = KRequestPending;
       
   253     iOperation = CImapConnectionOp::NewL(iMailboxApi.MsvSession(), KConnectedOpConnectPriority,
       
   254         iStatus, iService, iReporter, iConnectionCompletionState);
       
   255     SetActive();
       
   256     IMUM_OUT();
       
   257     }
       
   258 
       
   259 void CImap4ConnectedOp::DoDisconnectL()
       
   260     {
       
   261     IMUM_CONTEXT( CImap4ConnectedOp::DoDisconnectL, 0, KImumMtmLog );
       
   262     IMUM_IN();
       
   263     
       
   264     iStatus = KRequestPending;
       
   265     if(iDisconnect)
       
   266         {
       
   267         delete iOperation;
       
   268         iOperation = NULL;
       
   269         iOperation = CImumDisconnectOperation::NewL(
       
   270             iMailboxApi,
       
   271             iStatus,
       
   272             iReporter,
       
   273             iService,
       
   274             KIMAP4MTMDisconnect,
       
   275             KUidMsgTypeIMAP4);
       
   276         }
       
   277     else
       
   278         {
       
   279         CompleteThis();
       
   280         }
       
   281     SetActive();
       
   282     IMUM_OUT();
       
   283     }
       
   284 
       
   285