ipsservices/ipssosplugin/src/ipsplgpop3connectop.cpp
branchRCL_3
changeset 64 3533d4323edc
parent 63 d189ee25cf9d
--- a/ipsservices/ipssosplugin/src/ipsplgpop3connectop.cpp	Tue Aug 31 15:04:17 2010 +0300
+++ b/ipsservices/ipssosplugin/src/ipsplgpop3connectop.cpp	Wed Sep 01 12:28:57 2010 +0100
@@ -15,23 +15,20 @@
 *
 */
 
+
 #include "emailtrace.h"
 #include "ipsplgheaders.h"
 
+// Constants and defines
+const TInt KConnectOpPriority = CActive::EPriorityStandard;
+const TInt KIpsPlgPop3PopulateLimitInitValue = 50;
 
-// Constants and defines
-// <qmail> rename const
-const TInt KDefaultPopulateLimit( 50 );
-const TInt KPopulateAlgorithmBytesInKilo( 1024 );
-const TInt KPopulateAlgorithmRowLength( 75 );
-
+_LIT( KIpsPlgPopConnectPanic, "PopConnectOp" );
 
 // ----------------------------------------------------------------------------
 // CIpsPlgPop3ConnectOp::NewL()
 // ----------------------------------------------------------------------------
-// <qmail> MFSMailRequestObserver& changed to pointer
-// <qmail> aSignallingAllowed parameter added
-// <qmail> aFetchWillFollow parameter added
+//
 CIpsPlgPop3ConnectOp* CIpsPlgPop3ConnectOp::NewL(
     CMsvSession& aMsvSession,                           
     TRequestStatus& aObserverRequestStatus,
@@ -39,26 +36,18 @@
     TBool aForcePopulate,
     CIpsPlgTimerOperation& aActivityTimer,
     TFSMailMsgId aFSMailBoxId,
-    MFSMailRequestObserver* aFSOperationObserver,
+    MFSMailRequestObserver& aFSOperationObserver,
     TInt aFSRequestId,
     CIpsPlgEventHandler* aEventHandler,
-    TBool aSignallingAllowed,
-    TBool aFetchWillFollow )
+    TBool aSignallingAllowed )
     {
     FUNC_LOG;
-    CIpsPlgPop3ConnectOp* op = new(ELeave) CIpsPlgPop3ConnectOp(
-        aMsvSession,
-        aObserverRequestStatus,
-        aService,
-        aForcePopulate,
-        aActivityTimer,
-        aFSMailBoxId,
-        aFSOperationObserver,
-        aFSRequestId,
-        aEventHandler,
-        aSignallingAllowed,
-        aFetchWillFollow );
-
+    CIpsPlgPop3ConnectOp* op = 
+        new(ELeave) CIpsPlgPop3ConnectOp( aMsvSession, aObserverRequestStatus, 
+            aService, aForcePopulate, aActivityTimer, aFSMailBoxId,
+            aFSOperationObserver, aFSRequestId, 
+            aEventHandler, aSignallingAllowed );
+        
     CleanupStack::PushL( op );
     op->ConstructL();
     CleanupStack::Pop( op );
@@ -72,9 +61,10 @@
 CIpsPlgPop3ConnectOp::~CIpsPlgPop3ConnectOp()
     {
     FUNC_LOG;
-    // <qmail> removed iEntry;
-    // <qmail> removed iSelection;
-    // <qmail> removed state setting
+    delete iEntry;
+    delete iSelection;
+
+    iState = EIdle;
     }
 
 // ----------------------------------------------------------------------------
@@ -88,9 +78,9 @@
         {
         return GetErrorProgressL( iError );
         }
-    else if( iSubOperation )
+    else if( iOperation )
         {
-        return iSubOperation->ProgressL();
+        return iOperation->ProgressL();
         }
     
     iProgress().iErrorCode = KErrNone;
@@ -103,9 +93,9 @@
 void CIpsPlgPop3ConnectOp::DoCancel()
     {
     FUNC_LOG;
-    if( iSubOperation )
+    if( iOperation )
         {
-        iSubOperation->Cancel();
+        iOperation->Cancel();
         }
     CompleteObserver( KErrCancel );
     }
@@ -117,52 +107,73 @@
 void CIpsPlgPop3ConnectOp::DoRunL()
     {
     FUNC_LOG;
-    TInt err( KErrNone );
-
-    // <qmail> remove EQueryingDetails state
-    if ( iState == EStartConnect )
+    // handle these error situations properly, 
+    // and report somehow back to framework
+    TInt err  = KErrNone;
+ 
+    if ( iState == EQueryingDetails )
         {
-        if ( iSubOperation )
+        // Retry connect.
+        DoConnectL();
+        iState = EConnected;
+        }       
+    else if ( iState == EStartConnect )
+        {
+        
+        if ( iOperation )
             {
             // operation exist and it means 
             // disconnect operation was ongoing.
-            delete iSubOperation;
-            iSubOperation = NULL;
+            // How handle errors
+            delete iOperation;
+            iOperation = NULL;
             }
         
-// <qmail> ValidateL removed
-        // Begin Connect.
-        DoConnectL();
-        iState = EConnected;
+        if ( ValidateL() )
+            {
+            // Begin Connect.
+            DoConnectL();
+            iState = EConnected;
+            }
+        else
+            {
+            User::Leave( KErrCancel );    // User cancelled.
+            }
         }
     else if ( iState == EConnected )
         {
-        // Connect completed
-        // <qmail> Login details checking removed
+        // Connect completed.
         err = GetOperationErrorCodeL( );
-        User::LeaveIfError( err );
-
-        if ( iForcePopulate && Connected() )
+            
+        if ( ( err == KPop3InvalidUser ) ||
+             ( err == KPop3InvalidLogin )  ||
+             ( err == KPop3InvalidApopLogin ) )
             {
-            iState = EPopulate;
-            DoPopulateL();
+            // Login details are wrong. Trying to ask for password
+            if ( !QueryUserPassL() )
+                {
+                CompleteObserver( err );
+                }
             }
-        // <qmail>
-        else if( iFetchWillFollow && Connected() )
+        else if ( err == KErrNone )
             {
-            // Leave connection open to make fetch
-            // operation possible
-            iState = EIdle;
-            CompleteObserver( KErrNone );
+            if ( iForcePopulate && Connected() )
+                {
+                iState = EPopulate;
+                DoPopulateL();
+                }
+            else
+                {
+                iState = EIdle;
+                CompleteObserver( KErrNone );
+                }
             }
-        // </qmail>
         else
             {
-            // <qmail>
-            // start disconnecting
-            iState = EDisconnecting;
-            DoDisconnectL();
-            // </qmail>
+            // We can't do this before, because 
+            // we want to handle KPop3InvalidUser,
+            // KPop3InvalidLogin and KPop3InvalidApopLogin separately.
+            User::Leave( err );
             }
         }
     else if ( iState == EPopulate )
@@ -170,28 +181,18 @@
         err = GetOperationErrorCodeL( );
         if ( iEventHandler )
             {
-            iEventHandler->SetNewPropertyEvent( iService, KIpsSosEmailSyncCompleted, err );
+            iEventHandler->SetNewPropertyEvent( 
+                    iService, KIpsSosEmailSyncCompleted, err );
             }
-        CIpsPlgSyncStateHandler::SaveSuccessfulSyncTimeL( iMsvSession, iService );
-
-        // <qmail>
-        // start disconnecting
-        iState = EDisconnecting;
-        DoDisconnectL();
-        // </qmail>
+        CIpsPlgSyncStateHandler::SaveSuccessfulSyncTimeL(
+                iMsvSession, iService );
+        CompleteObserver( err ); 
         }
-    // <qmail>
-    else if ( iState == EDisconnecting )
-        {
-        // we're done here
-        CompleteObserver( KErrNone );
-        }
-    // </qmail>
-    // <qmail> removed state EErrInvalidDetails
     else
         {
         User::Panic( KIpsPlgPopConnectPanic ,KErrNotFound);
         }
+    
     }
 
 // ----------------------------------------------------------------------------
@@ -202,9 +203,9 @@
     {
     FUNC_LOG;
     iError = aError;
-    if ( iSubOperation && iError == KErrNone )
+    if ( iOperation && iError == KErrNone )
         {
-        return iSubOperation->ProgressL();
+        return iOperation->ProgressL();
         }
     TPop3Progress& progress = iProgress();
     progress.iPop3Progress = TPop3Progress::EPopConnecting;
@@ -225,10 +226,15 @@
 TFSProgress CIpsPlgPop3ConnectOp::GetFSProgressL() const
     {
     FUNC_LOG;
+    // might not never called, but gives something reasonable if called
     TFSProgress result = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
+    result.iError = KErrNone;
     switch( iState )
         {
-        // <qmail> removed queryingdetails cases
+        case EQueryingDetails:
+        case EQueryingDetailsBusy:
+            result.iProgressStatus = TFSProgress::EFSStatus_Authenticating;
+            break;
         case EStartConnect:
         case EConnected:
             result.iProgressStatus = TFSProgress::EFSStatus_Connecting;
@@ -254,11 +260,6 @@
 // CIpsPlgPop3ConnectOp::CIpsPlgPop3ConnectOp
 // ----------------------------------------------------------------------------
 //
-// <qmail> priority parameter has been removed
-// <qmail> MFSMailRequestObserver& changed to pointer
-// <qmail> aSignallingAllowed parameter added
-// <qmail> aFetchWillFollow parameter added
-// <qmail> iAlreadyConnected removed
 CIpsPlgPop3ConnectOp::CIpsPlgPop3ConnectOp(
     CMsvSession& aMsvSession,
     TRequestStatus& aObserverRequestStatus,
@@ -266,24 +267,21 @@
     TBool aForcePopulate,
     CIpsPlgTimerOperation& aActivityTimer,
     TFSMailMsgId aFSMailBoxId,
-    MFSMailRequestObserver* aFSOperationObserver,
+    MFSMailRequestObserver& aFSOperationObserver,
     TInt aFSRequestId,
     CIpsPlgEventHandler* aEventHandler,
-    TBool aSignallingAllowed,
-    TBool aFetchWillFollow )
+    TBool aSignallingAllowed )
     :
-    CIpsPlgOnlineOperation(
-        aMsvSession,
-        aObserverRequestStatus, 
-        aActivityTimer, 
-        aFSMailBoxId,
-        aFSOperationObserver, 
-        aFSRequestId,
-        aSignallingAllowed ),
+    CIpsPlgOnlineOperation( aMsvSession, KConnectOpPriority,
+        aObserverRequestStatus, aActivityTimer, aFSMailBoxId,
+        aFSOperationObserver, aFSRequestId, aSignallingAllowed ),
     iState( EIdle ),
+    iEntry( NULL ),
+    iPopulateLimit( KIpsPlgPop3PopulateLimitInitValue ),
     iForcePopulate( aForcePopulate ),
+    iSelection( NULL ),
     iEventHandler( aEventHandler ),
-    iFetchWillFollow( aFetchWillFollow )
+    iAlreadyConnected( EFalse )
     {
     iService = aServiceId; 
     }
@@ -296,36 +294,57 @@
     {
     FUNC_LOG;
     BaseConstructL( KUidMsgTypePOP3 );
-
-    // <qmail> iSelection construction has been removed
-
-    CMsvEntry* cEntry = iMsvSession.GetEntryL( iService );
-    User::LeaveIfNull( cEntry );
-    // NOTE: Not using cleanupStack as it is not needed in this situation:
-    const TMsvEntry tentry = cEntry->Entry();
-    delete cEntry;
-    cEntry = NULL;
-
+   	
+    iEntry = iMsvSession.GetEntryL( iService );
+    if ( !iEntry )
+        {
+        User::Leave( KErrGeneral );
+        }
+    iSelection = new(ELeave) CMsvEntrySelection;
+    const TMsvEntry& tentry = iEntry->Entry();
+    
     if ( tentry.iType.iUid != KUidMsvServiceEntryValue )
         {
         // should we panic with own codes?
         User::Leave( KErrNotSupported );
         }
-
-    // <qmail> no need to have population limit as member variable
-    // <qmail> it is read from settings when needed
     
-    if ( tentry.Connected() )
-        {      
-        iState = EConnected;
-        // <qmail> iAlreadyConnected removed
+    // get correct populate and sync limet values from settings
+    CImPop3Settings* settings = new(ELeave) CImPop3Settings();
+    CleanupStack::PushL( settings );
+    TPopAccount popAccountId;
+    CEmailAccounts* accounts = CEmailAccounts::NewLC();
+    accounts->GetPopAccountL( iService , popAccountId );
+    accounts->LoadPopSettingsL( popAccountId, *settings );
+    iPopulateLimit = settings->PopulationLimit();
+    if ( iPopulateLimit > 0 )
+        {
+        iPopulateLimit = ( iPopulateLimit * 1024 ) / 75;
+        }
+    else if ( iPopulateLimit == KIpsSetDataHeadersOnly )    
+        {
+        iPopulateLimit = 0;
         }
     else
         {
-        iState = EStartConnect;
+        iPopulateLimit = KMaxTInt;
         }
-    // <qmail> SetActive(); moved inside CompleteThis();
-    CompleteThis();
+    
+    CleanupStack::PopAndDestroy( 2, settings );
+       
+    if ( tentry.Connected() )
+        {      
+        iState = EConnected; 
+        iAlreadyConnected = ETrue;
+        SetActive();
+        CompleteThis();
+        }
+    else
+        {
+        iState = EStartConnect; 
+        SetActive();
+        CompleteThis();
+        }    
     }
 
 // ----------------------------------------------------------------------------
@@ -335,17 +354,9 @@
 void CIpsPlgPop3ConnectOp::DoConnectL()
     {
     FUNC_LOG;
-    // <qmail> unnecessary: iStatus = KRequestPending;
-    NM_COMMENT("CIpsPlgPop3ConnectOp: connecting");
-    InvokeClientMtmAsyncFunctionL( KPOP3MTMConnect, iService ); // <qmail> 1 param removed
+    iStatus = KRequestPending;
+    InvokeClientMtmAsyncFunctionL( KPOP3MTMConnect, iService, iService );
     SetActive();
-
-// <qmail>
-    if ( iEventHandler )
-        {
-        iEventHandler->SetNewPropertyEvent( iService, KIpsSosEmailSyncStarted, KErrNone );
-        }
-// </qmail>
     }
 
 // ----------------------------------------------------------------------------
@@ -355,99 +366,149 @@
 void CIpsPlgPop3ConnectOp::DoPopulateL()
     {
     FUNC_LOG;
-    NM_COMMENT("CIpsPlgPop3ConnectOp: populating");
-    // <qmail> unnecessary: iStatus = KRequestPending;
+    iStatus = KRequestPending;
 
     // Prepare parameters and include filtering
     TImPop3PopulateOptions pop3GetMailInfo;
     pop3GetMailInfo.SetMaxEmailSize( KMaxTInt32 );
-// <qmail>
-    pop3GetMailInfo.SetPopulationLimit( GetPopulateLimitFromSettingsL() );
+    pop3GetMailInfo.SetPopulationLimit( iPopulateLimit );
     TPckgBuf<TImPop3PopulateOptions> params( pop3GetMailInfo );
 
-    // <qmail> selection is no longer a member variable
-    CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
-    CleanupStack::PushL( selection );
-    selection->InsertL( 0, iService );
+    iSelection->InsertL(0, iService);
     iBaseMtm->SwitchCurrentEntryL( iService );
     // Start the fetch operation
-    InvokeClientMtmAsyncFunctionL( KPOP3MTMPopulateAll, *selection, params ); // <qmail> 1 param removed
+    InvokeClientMtmAsyncFunctionL( KPOP3MTMPopulateAll, *iSelection,
+        iService, params);
+
     SetActive();
-    CleanupStack::PopAndDestroy( selection );
-// <qmail> iEventHandler->SetNewPropertyEvent call removed from here
-// </qmail>
+
+    if ( iEventHandler )
+        {
+        iEventHandler->SetNewPropertyEvent( 
+                iService, KIpsSosEmailSyncStarted, KErrNone );
+        } 
     }
-// <qmail> Connected() moved to baseclass
+// ----------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+//
+TBool CIpsPlgPop3ConnectOp::Connected() const
+    {
+    FUNC_LOG;
+    TMsvEntry tentry;
+    TMsvId service;
+    iMsvSession.GetEntry(iService, service, tentry );
+    return tentry.Connected();
+    }
 
 // ----------------------------------------------------------------------------
+// CIpsPlgImap4ConnectOp::IpsOpType()
 // ----------------------------------------------------------------------------
-// <qmail> return type
-TIpsOpType CIpsPlgPop3ConnectOp::IpsOpType() const
+// 
+TInt CIpsPlgPop3ConnectOp::IpsOpType() const
     {
     FUNC_LOG;
     return EIpsOpTypePop3SyncOp;
     }
 
-// <qmail> removed QueryUsrPassL() from here as unneeded
-// <qmail> removed ValidateL() (did nothing)
+
+// ----------------------------------------------------------------------------
+// CIpsPlgPop3ConnectOp::ValidateL()
+// ----------------------------------------------------------------------------
+//
+TBool CIpsPlgPop3ConnectOp::ValidateL()
+    {
+    // do proper validation. OR: not needed?
+    return ETrue;
+    }
 
 // ----------------------------------------------------------------------------
+// CIpsPlgPop3ConnectOp::QueryUserPassL()
+// ----------------------------------------------------------------------------
+//
+TBool CIpsPlgPop3ConnectOp::QueryUserPassL()
+    {
+    FUNC_LOG;
+
+    if ( iEventHandler )
+        {
+        // ask for credentials for pop3 account and wait for callback
+        if ( iEventHandler->QueryUsrPassL( iEntry->EntryId(), this ) )
+            {
+            iState = EQueryingDetails;
+            }
+        else
+            {
+            // another operation is waiting for password
+            iState = EQueryingDetailsBusy;
+            }
+
+        return ETrue;
+        }
+
+    return EFalse;
+    }
+
+
+// ----------------------------------------------------------------------------
+// CIpsPlgPop3ConnectOp::GetOperationErrorCodeL()
 // ----------------------------------------------------------------------------    
 // 
 TInt CIpsPlgPop3ConnectOp::GetOperationErrorCodeL( )
     {
     FUNC_LOG;
-    if ( !iSubOperation )
+    
+    if ( iAlreadyConnected )
+        {
+        // Connected state was set in CIpsPlgPop3ConnectOp::ConstructL()
+        // so iOperation is null
+        return KErrNone;
+        }
+        
+    if ( !iOperation )
         {
         return KErrNotFound;
         }
-    if ( !iSubOperation->IsActive() && iSubOperation->iStatus.Int() != KErrNone )
+    if ( !iOperation->IsActive() && iOperation->iStatus.Int() != KErrNone )
         {
-        return iSubOperation->iStatus.Int();
+        return iOperation->iStatus.Int();
         }
     
     TPckgBuf<TPop3Progress> paramPack;
-    paramPack.Copy( iSubOperation->ProgressL() );
+    paramPack.Copy( iOperation->ProgressL() );
     const TPop3Progress& progress = paramPack();
     
     return progress.iErrorCode;
     }
 
-// <qmail> removed CIpsPlgImap4ConnectOp::CredientialsSetL
-
-//<qmail> new functions
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
-void CIpsPlgPop3ConnectOp::DoDisconnectL()
-    {
-    FUNC_LOG;
-    // <qmail> unnecessary: iStatus = KRequestPending;
-    NM_COMMENT("CIpsPlgPop3ConnectOp: disconnecting");
-    InvokeClientMtmAsyncFunctionL( KPOP3MTMDisconnect, iService ); // <qmail> 1 param removed
-    SetActive();
-    }
-
-// ----------------------------------------------------------------------------
-// ----------------------------------------------------------------------------
-TInt CIpsPlgPop3ConnectOp::GetPopulateLimitFromSettingsL()
+void CIpsPlgPop3ConnectOp::CredientialsSetL( TInt aEvent )
     {
     FUNC_LOG;
-    TInt limit( KDefaultPopulateLimit );
-        
-    CImPop3Settings* settings = new(ELeave) CImPop3Settings();
-    CleanupStack::PushL( settings );
-    TPopAccount popAccountId;
-    CEmailAccounts* accounts = CEmailAccounts::NewLC();
-    accounts->GetPopAccountL( iService , popAccountId );
-    accounts->LoadPopSettingsL( popAccountId, *settings );
-    limit = settings->PopulationLimit();
-    if ( limit > 0 )
+
+    if ( iState == EQueryingDetails )
         {
-        // basically doing a _very_rough_ conversion from kilobyte value to number-of-rows
-        limit = ( limit * KPopulateAlgorithmBytesInKilo ) / KPopulateAlgorithmRowLength;
+        // response for our operation`s query
+        if ( aEvent == EIPSSosCredientialsCancelled )
+            {
+            // user canceled operation
+            CompleteObserver( KErrCancel );
+            }
+
+        // password has been set, continue with operation
+        SetActive();
+        CompleteThis();
         }
-    CleanupStack::PopAndDestroy( 2, settings );
-    return limit;
+    else if ( iState == EQueryingDetailsBusy )
+        {
+        // response for other operation`s query
+        // we could try to ask for password now,
+        // but decision was made to cancel operation
+        CompleteObserver( KErrCancel );
+        SetActive();
+        CompleteThis();
+        }
     }
+//EOF
 
-//</qmail>
+