email/imum/Mtms/Src/MsvPop3ConnectOp.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 *       Encapsulates connection validation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <eikenv.h>
       
    21 
       
    22 #include "ImumPanic.h"
       
    23 #include "MsvPop3ConnectOp.h"
       
    24 #include <ImumUtils.rsg>
       
    25 #include <miut_err.h>
       
    26 #include <SendUiConsts.h>
       
    27 #include <ImumInMailboxUtilities.h>
       
    28 
       
    29 #include "MsvConnectionValidation.h"
       
    30 #include "MsvEmailConnectionProgressProvider.h"
       
    31 #include "EmailUtils.H"
       
    32 #include "ImumDisconnectOperation.h"
       
    33 #include "ImumMtmLogging.h"
       
    34 #include "ImumMboxSettingsUtils.h"
       
    35 
       
    36 #include <MsvPrgReporter.h>
       
    37 
       
    38 // Constants and defines
       
    39 const TInt KConnectOpPriority = CActive::EPriorityStandard;
       
    40 const TInt KImumMaxLoginRetries = 3;
       
    41 
       
    42 CMsvPop3ConnectOp* CMsvPop3ConnectOp::NewL(
       
    43     TRequestStatus& aObserverRequestStatus,
       
    44     MMsvProgressReporter& aReporter,
       
    45     TMsvId aService,
       
    46     TInt aPopulateLimit )
       
    47     {
       
    48     IMUM_STATIC_CONTEXT( CMsvPop3ConnectOp::NewL, 0, mtm, KImumMtmLog );
       
    49     IMUM_IN();
       
    50 
       
    51     // Normally this would be done in ConstructL method, but the base class
       
    52     // requires this instance to exist in constructor call. We don't want to
       
    53     // use the MTM instance of the emailAPI because this operation can outlive
       
    54     // the MTM.
       
    55     CImumInternalApi* emailApi = CreateEmailApiLC();
       
    56 
       
    57     CMsvPop3ConnectOp* op = new(ELeave) CMsvPop3ConnectOp( emailApi,
       
    58         aObserverRequestStatus,
       
    59         aReporter,
       
    60         aService,
       
    61         aPopulateLimit);
       
    62 
       
    63     CleanupStack::Pop( emailApi ); // ownership transferred to operation
       
    64     CleanupStack::PushL(op);
       
    65     op->ConstructL();
       
    66     CleanupStack::Pop();
       
    67     IMUM_OUT();
       
    68     return op;
       
    69     }
       
    70 
       
    71 CMsvPop3ConnectOp::~CMsvPop3ConnectOp()
       
    72     {
       
    73     IMUM_CONTEXT( CMsvPop3ConnectOp::~CMsvPop3ConnectOp, 0, KImumMtmLog );
       
    74     IMUM_IN();
       
    75 
       
    76     delete iEntry;
       
    77     delete iProgProvider;
       
    78     delete iSelection;
       
    79     delete iMboxApi;
       
    80     IMUM_OUT();
       
    81     }
       
    82 
       
    83 const TDesC8& CMsvPop3ConnectOp::ProgressL()
       
    84     {
       
    85     IMUM_CONTEXT( CMsvPop3ConnectOp::ProgressL, 0, KImumMtmLog );
       
    86     IMUM_IN();
       
    87 
       
    88     //
       
    89     // If refreshing the inbox, return client op progress, else use connection progress provider.
       
    90     //
       
    91     if ( iError != KErrNone )
       
    92         {
       
    93         if ( FlagIsSet( EIapWasInvalid ) )
       
    94             {
       
    95             iError = KPop3CannotConnect;
       
    96             }
       
    97 
       
    98         return GetErrorProgressL( iError );
       
    99         }
       
   100     if ( !iOperation )
       
   101         {
       
   102         IMUM_OUT();
       
   103         return iDummyProg;
       
   104         }
       
   105     iProgress.Copy( iOperation->ProgressL() );
       
   106     if ( iStatus == KRequestPending )
       
   107         {
       
   108         if ( iProgress().iPop3Progress == TPop3Progress::EPopRefreshing ||
       
   109              iProgress().iPop3Progress == TPop3Progress::EPopTopPopulating ||
       
   110              iProgress().iPop3Progress == TPop3Progress::EPopPopulating )
       
   111             {
       
   112             IMUM_OUT();
       
   113             // Refreshing inbox or doing populate
       
   114             return iProgress;
       
   115             }
       
   116 
       
   117         // Operation not completed, and not refreshing inbox.
       
   118         return iProgProvider->GetConnectionProgressL( iError );
       
   119         }
       
   120     else if ( FlagIsSet( EMaxNumberOfLoginTries ) )
       
   121         {
       
   122         // User has entered wrong username or password max number of times, display error note
       
   123         iProgress().iErrorCode = KPop3InvalidLogin;
       
   124         IMUM_OUT();
       
   125         return iProgress;
       
   126         }
       
   127     else if ( FlagIsSet( EInvalidDetails ) )
       
   128         {
       
   129         // User details are wrong.
       
   130         // This error is handled using a special dialog in DoRunL() which allows retrying of connection.
       
   131         // The user must have cancelled that dialog, so patch the error code so we don't display another
       
   132         // error notifier in CPop3MtmUi::DisplayProgressSummary().
       
   133         // iOperation is a Client MTM connect op, progress is a TPop3Progress.
       
   134         iProgress().iErrorCode = KErrCancel;
       
   135         IMUM_OUT();
       
   136         return iProgress;
       
   137         }
       
   138     IMUM_OUT();
       
   139     // Operation completed.
       
   140     return iProgress;
       
   141     }
       
   142 
       
   143 void CMsvPop3ConnectOp::DoCancel()
       
   144     {
       
   145     IMUM_CONTEXT( CMsvPop3ConnectOp::DoCancel, 0, KImumMtmLog );
       
   146 
       
   147     CImumOnlineOperation::DoCancel();
       
   148     IMUM_OUT();
       
   149     }
       
   150 
       
   151 // ----------------------------------------------------------------------------
       
   152 // CMsvPop3ConnectOp::DoRunL()
       
   153 // ----------------------------------------------------------------------------
       
   154 //
       
   155 void CMsvPop3ConnectOp::DoRunL()
       
   156     {
       
   157     IMUM_CONTEXT( CMsvPop3ConnectOp::DoRunL, 0, KImumMtmLog );
       
   158     IMUM_IN();
       
   159 
       
   160     TInt err  = KErrNone;
       
   161 
       
   162     if ( FlagIsSet( EDoConnect ) )
       
   163         {
       
   164         // To speed up connection establishment, avoid loading settings multiple
       
   165         // times. Get all the info needed at once.
       
   166         CImumInSettingsData* settings =
       
   167             iMailboxApi.MailboxServicesL().LoadMailboxSettingsL( iService );
       
   168         CleanupStack::PushL( settings );
       
   169         // Get iap name
       
   170         TBuf<KImasImailMaxSettingsLength> iapName;
       
   171         MsvConnectionValidation::GetServiceIapNameL( *settings,
       
   172             KSenduiMtmPop3Uid, iapName);
       
   173         TBool ok = ValidateL( *settings );
       
   174         CleanupStack::PopAndDestroy(); // settings
       
   175         if ( ok )
       
   176             {
       
   177             // Begin Connect.
       
   178             UnsetFlag( EDoConnect );
       
   179             iProgProvider = CMsvEmailConnectionProgressProvider::NewL(
       
   180                 iapName, iService );
       
   181             DoConnectL();
       
   182             IMUM_OUT();
       
   183             return;
       
   184             }
       
   185         else
       
   186             {
       
   187             err = KErrCancel;    // User cancelled.
       
   188             }
       
   189         }
       
   190     else
       
   191         {
       
   192         // Connect completed.
       
   193         err = GetOperationCompletionCodeL();
       
   194         if ( ( err == KPop3InvalidUser ) ||
       
   195              ( err == KPop3InvalidLogin )  ||
       
   196              ( err == KPop3InvalidApopLogin ) )
       
   197             {
       
   198             // Login details are wrong.
       
   199             TBool retry = EFalse;
       
   200             if ( iLoginRetryCounter < KImumMaxLoginRetries )
       
   201                 {
       
   202                 TRAP( err, retry =
       
   203                     MsvConnectionValidation::ShowLoginDetailsRejectionDlgL(
       
   204                     iMailboxApi, iService ) );
       
   205                 }
       
   206             else
       
   207                 {
       
   208                 SetFlag( EMaxNumberOfLoginTries );
       
   209                 }
       
   210             iLoginRetryCounter++;
       
   211 
       
   212             if ( retry )
       
   213                 {
       
   214                 // Retry connect.
       
   215                 ReDisplayProgressL(R_EMAIL_CONNECTING_SERVER, iService);
       
   216                 delete iOperation;
       
   217                 iOperation = NULL;
       
   218                 DoConnectL();
       
   219                 IMUM_OUT();
       
   220                 return;
       
   221                 }
       
   222             else
       
   223                 {
       
   224                 SetFlag( EInvalidDetails );
       
   225                 }
       
   226             }
       
   227         else if ( err != KErrNone )
       
   228             {
       
   229             // We can't do this before, because we want to handle KPop3InvalidUser,
       
   230             // KPop3InvalidLogin and KPop3InvalidApopLogin separately.
       
   231             User::Leave( err );
       
   232             }
       
   233         else
       
   234             {
       
   235             if ( !iPopulated &&
       
   236                 !FlagIsSet( EDoingPopulate ) &&
       
   237                 ( iPopulateLimit == -1 || iPopulateLimit > 0 ) )
       
   238                 {
       
   239             	ImumMboxSettingsUtils::SetLastUpdateInfoL(
       
   240             	    iMailboxApi,
       
   241                 	iService,
       
   242                 	err == KErrNone && !FlagIsSet( EInvalidDetails ) );
       
   243                 //start populate
       
   244                 DoPopulateL();
       
   245                 IMUM_OUT();
       
   246                 return;
       
   247                 }
       
   248             if ( FlagIsSet( EDoingPopulate ) )
       
   249                 {
       
   250                 UnsetFlag( EDoingPopulate );
       
   251                 }
       
   252             }
       
   253         /* Background Handling */
       
   254         MsvEmailMtmUiUtils::CallNewMessagesL( iService );
       
   255         }
       
   256     CompleteObserver( err );
       
   257     IMUM_OUT();
       
   258     }
       
   259 
       
   260 const TDesC8& CMsvPop3ConnectOp::GetErrorProgressL(TInt aError)
       
   261     {
       
   262     IMUM_CONTEXT( CMsvPop3ConnectOp::GetErrorProgressL, 0, KImumMtmLog );
       
   263     IMUM_IN();
       
   264 
       
   265     iError = aError;
       
   266     if ( iOperation && iError == KErrNone )
       
   267         {
       
   268         IMUM_OUT();
       
   269         return iOperation->ProgressL();
       
   270         }
       
   271     TPop3Progress& progress = iProgress();
       
   272     progress.iPop3Progress = TPop3Progress::EPopConnecting;
       
   273     progress.iTotalMsgs = 0;
       
   274     progress.iMsgsToProcess = 0;
       
   275     progress.iBytesDone = 0;
       
   276     progress.iTotalBytes = 0;
       
   277     progress.iErrorCode = iError;
       
   278     progress.iPop3SubStateProgress = TPop3Progress::EPopConnecting;
       
   279     progress.iServiceId = iService;
       
   280     IMUM_OUT();
       
   281     return iProgress;
       
   282     }
       
   283 
       
   284 CMsvPop3ConnectOp::CMsvPop3ConnectOp(
       
   285     CImumInternalApi* aMailboxApi,
       
   286     TRequestStatus& aObserverRequestStatus,
       
   287     MMsvProgressReporter& aReporter,
       
   288     TMsvId aServiceId,
       
   289     TInt aPopulateLimit)
       
   290     :
       
   291     CImumOnlineOperation(
       
   292     *aMailboxApi,
       
   293     KConnectOpPriority,
       
   294     aObserverRequestStatus,
       
   295     aReporter)
       
   296     {
       
   297     IMUM_CONTEXT( CMsvPop3ConnectOp::CMsvPop3ConnectOp, 0, KImumMtmLog );
       
   298     IMUM_IN();
       
   299     iMboxApi = aMailboxApi;
       
   300     iService = aServiceId;
       
   301     iPopulateLimit = aPopulateLimit;
       
   302     IMUM_OUT();
       
   303     }
       
   304 
       
   305 void CMsvPop3ConnectOp::ConstructL()
       
   306     {
       
   307     IMUM_CONTEXT( CMsvPop3ConnectOp::ConstructL, 0, KImumMtmLog );
       
   308     IMUM_IN();
       
   309 
       
   310     iEntry = iMsvSession.GetEntryL(iService);
       
   311     iSelection = new(ELeave) CMsvEntrySelection;
       
   312     const TMsvEntry& tentry = iEntry->Entry();
       
   313     __ASSERT_ALWAYS( tentry.iType.iUid == KUidMsvServiceEntryValue,
       
   314         User::Panic(KImumMtmUiPanic,EPop3MtmUiOpConnectNotAService) );
       
   315     BaseConstructL(KUidMsgTypePOP3);
       
   316 
       
   317     TBool ok = ETrue;
       
   318     if(tentry.Connected())
       
   319         {
       
   320         // We are already connected.
       
   321         ok = EFalse;
       
   322 
       
   323         //if connected, we must have populated already
       
   324         iPopulated = ETrue;
       
   325         }
       
   326     if(ok)
       
   327         {
       
   328         // User did not cancel connection, and we are not connected.
       
   329         SetFlag(EDoConnect);
       
   330         }
       
   331     SetActive();
       
   332     CompleteThis();
       
   333     IMUM_OUT();
       
   334     }
       
   335 
       
   336 void CMsvPop3ConnectOp::DoConnectL()
       
   337     {
       
   338     IMUM_CONTEXT( CMsvPop3ConnectOp::DoConnectL, 0, KImumMtmLog );
       
   339     IMUM_IN();
       
   340 
       
   341     // Begin connect.
       
   342     iStatus = KRequestPending;
       
   343     InvokeClientMtmAsyncFunctionL(KPOP3MTMConnect, iService, iService);
       
   344     SetActive();
       
   345     }
       
   346 
       
   347 void CMsvPop3ConnectOp::DoPopulateL()
       
   348     {
       
   349     IMUM_CONTEXT( CMsvPop3ConnectOp::DoPopulateL, 0, KImumMtmLog );
       
   350     IMUM_IN();
       
   351 
       
   352     // Populate only if it hasn't done yet
       
   353     if( !iPopulated )
       
   354         {
       
   355         iStatus = KRequestPending;
       
   356 
       
   357         // Prepare parameters and include filtering
       
   358         TImPop3PopulateOptions pop3GetMailInfo;
       
   359         pop3GetMailInfo.SetMaxEmailSize( KMaxTInt32 );
       
   360         pop3GetMailInfo.SetPopulationLimit( iPopulateLimit );
       
   361         TPckg<TImPop3PopulateOptions> params(pop3GetMailInfo);
       
   362 
       
   363         iSelection->InsertL(0, iService);
       
   364 
       
   365         // Start the fetch operation
       
   366         InvokeClientMtmAsyncFunctionL(
       
   367             KPOP3MTMPopulateNew,
       
   368             *iSelection,
       
   369             iService,
       
   370             params);
       
   371         SetFlag(EDoingPopulate);
       
   372 
       
   373         // Make sure that this won't be done too many times
       
   374         iPopulated = ETrue;
       
   375         }
       
   376     SetActive();
       
   377     IMUM_OUT();
       
   378     }
       
   379 
       
   380 
       
   381 TBool CMsvPop3ConnectOp::ValidateL( CImumInSettingsData& aSettings )
       
   382     {
       
   383     IMUM_CONTEXT( CMsvPop3ConnectOp::ValidateL, 0, KImumMtmLog );
       
   384     IMUM_IN();
       
   385 
       
   386     iLoginRetryCounter = 0;
       
   387     TBool ok = ( aSettings.Validate() == KErrNone );
       
   388     if ( !ok )
       
   389         {
       
   390         SetFlag( EIapWasInvalid );
       
   391         }
       
   392     else
       
   393         {
       
   394         UnsetFlag( EIapWasInvalid );
       
   395         }
       
   396 
       
   397     if(ok)
       
   398         {
       
   399         iReporter.MakeProgressVisibleL(ETrue);
       
   400         TMsvLoginData loginData;
       
   401         ImumMboxSettingsUtils::GetLoginInformationL( aSettings,
       
   402             KSenduiMtmImap4Uid, loginData.iUsername, loginData.iPassword,
       
   403             loginData.iIsPasswordTemporary );
       
   404         ok = MsvConnectionValidation::CheckAndPromptForPasswordL(
       
   405                 iMailboxApi, iService, loginData, iLoginRetryCounter );
       
   406         }
       
   407 
       
   408     IMUM_OUT();
       
   409     return ok;
       
   410     }