email/alwaysonlineemailplugin/src/AlwaysOnlinePopAgent.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 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 *     POP specific email agent
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //INCLUDES
       
    21 #include <SenduiMtmUids.h>
       
    22 #include <es_sock.h>
       
    23 #include <MuiuOperationWait.h>
       
    24 #include <ImumInSettingsKeys.h>
       
    25 #include <ImumDaSettingsKeys.h>
       
    26 
       
    27 #include "AlwaysOnlinePopAgent.h"
       
    28 #include "AlwaysOnlineEmailPluginLogging.h"
       
    29 #include "AlwaysOnlineEmailLoggingTools.h"
       
    30 const TInt KEmailMaxCharsInLine = 75;
       
    31 const TInt KEmailKB = 1024;
       
    32 
       
    33 // ----------------------------------------------------------------------------
       
    34 // CAlwaysOnlinePop3Agent()
       
    35 // ----------------------------------------------------------------------------
       
    36 CAlwaysOnlinePop3Agent::CAlwaysOnlinePop3Agent( 
       
    37     CMsvSession& aSession,
       
    38     CClientMtmRegistry& aClientMtmRegistry,
       
    39     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager,
       
    40     CAlwaysOnlineEmailAgent& aEmailAgent )
       
    41     :
       
    42     CAlwaysOnlineEmailAgentBase( 
       
    43         aSession, aClientMtmRegistry, 
       
    44         aAlwaysOnlineManager, aEmailAgent )
       
    45     {
       
    46     }
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // NewL
       
    50 // ----------------------------------------------------------------------------
       
    51 CAlwaysOnlinePop3Agent* CAlwaysOnlinePop3Agent::NewL( 
       
    52     CMsvSession& aSession,
       
    53     CClientMtmRegistry& aClientMtmRegistry,
       
    54     TMsvId aMailboxId,
       
    55     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager,
       
    56     CAlwaysOnlineEmailAgent& aEmailAgent )
       
    57     {
       
    58     AOLOG_IN( "CAlwaysOnlinePop3Agent::NewL" );
       
    59     CAlwaysOnlinePop3Agent* self = new(ELeave)CAlwaysOnlinePop3Agent( 
       
    60         aSession, aClientMtmRegistry, aAlwaysOnlineManager, aEmailAgent );
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aMailboxId );
       
    63     CleanupStack::Pop( self );
       
    64     return self;
       
    65     }
       
    66 
       
    67 
       
    68 // ----------------------------------------------------------------------------
       
    69 // ConstructL()
       
    70 // ----------------------------------------------------------------------------
       
    71 void CAlwaysOnlinePop3Agent::ConstructL( TMsvId aMailboxId )
       
    72     {
       
    73     AOLOG_IN( "CAlwaysOnlinePop3Agent::ConstructL" );
       
    74     CAlwaysOnlineEmailAgentBase::ConstructL( aMailboxId );
       
    75     iPop3ClientMtm = (CPop3ClientMtm*) iClientMtmRegistry.NewMtmL( 
       
    76         KSenduiMtmPop3Uid );
       
    77     iFlags->ClearFlag( EAOBFIsImap4 );    
       
    78     }
       
    79 
       
    80 // ----------------------------------------------------------------------------
       
    81 // ~CAlwaysOnlinePop3Agent()
       
    82 // ----------------------------------------------------------------------------
       
    83 CAlwaysOnlinePop3Agent::~CAlwaysOnlinePop3Agent()
       
    84     {
       
    85     AOLOG_IN( "CAlwaysOnlinePop3Agent::~CAlwaysOnlinePop3Agent" );
       
    86     // cancel ongoing operations
       
    87     CloseServices();
       
    88     delete iPop3ClientMtm;
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // StartL()
       
    93 // ----------------------------------------------------------------------------
       
    94 void CAlwaysOnlinePop3Agent::StartL()
       
    95     {
       
    96     AOLOG_IN( "CAlwaysOnlinePop3Agent::StartL" );
       
    97     //once start delay op has been started we don't ever reset that id.
       
    98     if ( iStartDelayOpId == KErrNotFound )
       
    99         {
       
   100         //check offline & roaming statuses from AO server
       
   101         TBool suspended = EFalse;
       
   102         QueryAndHandleAOServerInfoL( suspended );
       
   103         
       
   104         if ( suspended )
       
   105             {
       
   106             //we got iState = EEmailAgentIdle from CAlwaysOnlineEmailAgentBase::Suspend
       
   107             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::StartL() We have been suspended, not starting!");
       
   108             return;
       
   109             }
       
   110 
       
   111         iState = EEmailAgentInitialised;
       
   112         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartL. Setting state: %d ",iState );
       
   113 
       
   114     
       
   115         TTime home;
       
   116         home.HomeTime();
       
   117 
       
   118         //Let's not start immediately on start. Small delay is safer
       
   119         TTimeIntervalSeconds startDelay( KInitWaitSeconds );
       
   120         home += startDelay;
       
   121 
       
   122         StartTimerOperationL( home, iStartDelayOpId );
       
   123         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartL(). Activated startDelayWait 5sec Op Id: %d ",iStartDelayOpId );
       
   124         }//if
       
   125     else
       
   126         {
       
   127         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::StartL() called, but we're allready started. No changes to state machine");
       
   128         }
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // ConnectAndUpdateHeadersL()
       
   133 // ----------------------------------------------------------------------------
       
   134 void CAlwaysOnlinePop3Agent::ConnectAndUpdateHeadersL()
       
   135     {
       
   136     AOLOG_IN( "CAlwaysOnlinePop3Agent::ConnectAndUpdateHeadersL" );
       
   137     KAOEMAIL_LOGGER_WRITE_FORMAT( "CAlwaysOnlinePop3Agent::ConnectAndUpdateHeadersL(). Starting to connect into: %d", iEntry->Entry().Id() );
       
   138     CreatePop3OperationL( iConnectOpId, KPOP3MTMConnect, EFalse );
       
   139     iState = EEmailAgentConnecting;
       
   140     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::ConnectAndUpdateHeadersL(). Setting state:  %d ",iState );
       
   141     }
       
   142 
       
   143 // ----------------------------------------------------------------------------
       
   144 // DisconnectL()
       
   145 // ----------------------------------------------------------------------------
       
   146 void CAlwaysOnlinePop3Agent::DisconnectL( TBool aAutomatic )
       
   147     {
       
   148     AOLOG_IN( "CAlwaysOnlinePop3Agent::DisconnectL" );    
       
   149     TBuf8<1> dummyParams;
       
   150     dummyParams.Zero();
       
   151 
       
   152     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL(*this);
       
   153     CleanupStack::PushL( watcher );
       
   154     CMsvOperation* op = NULL;
       
   155 
       
   156     if ( iEntry->Entry().Connected() )
       
   157         {
       
   158         CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   159         CleanupStack::PushL( selection );
       
   160         selection->AppendL( iEntry->Entry().iServiceId );
       
   161         iPop3ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   162         op = iPop3ClientMtm->InvokeAsyncFunctionL( 
       
   163             KPOP3MTMDisconnect, *selection, dummyParams, watcher->iStatus);
       
   164 
       
   165         CleanupStack::PopAndDestroy( selection );//selection
       
   166         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::DisconnectL: ADDING DISCONNECT OPERATION: %d", op->Id() );
       
   167         }
       
   168     else 
       
   169         {
       
   170         //already disconnected
       
   171         op = CMsvCompletedOperation::NewL(
       
   172                 iSession, iEntry->Entry().iMtm, KNullDesC8, 
       
   173                 KMsvLocalServiceIndexEntryId, 
       
   174                 watcher->iStatus, KErrCancel);
       
   175         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::DisconnectL: ADDING COMPLETED OPERATION: %d", op->Id() );
       
   176         }
       
   177 
       
   178     CleanupStack::PushL( op );
       
   179     AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
   180     CleanupStack::Pop( 2, watcher); // op // CSI: 12,47 # nothing wrong
       
   181 
       
   182     if ( aAutomatic )
       
   183         {
       
   184         iState = EEmailAgentAutoDisconnecting;
       
   185         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::DisconnectL. Automatic, setting state:  %d ",iState );
       
   186         }
       
   187     else
       
   188         {
       
   189         iState = EEmailAgentUserDisconnecting;
       
   190         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::DisconnectL. Setting state:  %d ",iState );
       
   191         }
       
   192     }
       
   193 
       
   194 
       
   195 // ----------------------------------------------------------------------------
       
   196 // ChangeNextStateL
       
   197 // ----------------------------------------------------------------------------
       
   198 void CAlwaysOnlinePop3Agent::ChangeNextStateL()
       
   199     {
       
   200     AOLOG_IN( "CAlwaysOnlinePop3Agent::ChangeNextStateL" ); 
       
   201     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::ChangeNextStateL() iState: %d", iState);
       
   202     
       
   203     //if we are waiting for schedule to start, we don't handle conn errors.
       
   204     //this should only occur if conn op has been started just before schedule ends.
       
   205     if ( iError != KErrNone && iState != EEmailAgentTimerWaitingForStart )
       
   206         {
       
   207         HandleOpErrorL();
       
   208         }
       
   209 
       
   210     switch ( iState )
       
   211         {
       
   212         //agent just constructed ( or settings changed ) and ready. Should be here only when settings have be saved
       
   213         //with always / home settings
       
   214         case EEmailAgentInitialised:
       
   215             //settings may have been changed, reset all pending operations
       
   216             //because their timings can be different compared to newly saved settings
       
   217 
       
   218             ResetAll();
       
   219             
       
   220             if ( !IsEmn() )
       
   221                 {
       
   222                 CheckAndHandleSchedulingL();//starts schedule timers and sets state accordingly
       
   223                 if( iWaitForStopOpId != KErrNotFound || 
       
   224                     ( iWaitForStopOpId == KErrNotFound && 
       
   225                       iWaitForStartOpId == KErrNotFound ) )//no start or stop waiters, all times
       
   226                     {
       
   227                     //CheckAndHandleCheduling started waitForStopTimer
       
   228                     StartWaitTimerL();
       
   229                     }
       
   230                 //else, do nothing, we wait until waitForStartTimer expires and starts updates again
       
   231                 }
       
   232             else
       
   233                 {
       
   234                 iState = EEmailAgentIdle;
       
   235                 CreateCompletedOpL();
       
   236                 KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::ChangeNextStateL() EMN is on --> no scheduling or timers needed!");
       
   237                 }
       
   238             break;
       
   239 
       
   240         //case EEmailAgentConnected:
       
   241         case EEmailAgentConnecting:
       
   242             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::ChangeNextStateL() DoFilteredPopulateL");
       
   243             //if filter not used, just moves to next state - disconnect
       
   244             DoFilteredPopulateL();
       
   245             break;
       
   246         case EEmailAgentDoingFilteredPopulate:
       
   247             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::ChangeNextStateL() DisconnectL");
       
   248             DisconnectL( ETrue );
       
   249             break;
       
   250         case EEmailAgentConnectFailed:
       
   251         case EEmailAgentQueued:
       
   252         case EEmailAgentReconnecting:
       
   253         case EEmailAgentAutoDisconnecting:
       
   254         case EEmailAgentUserDisconnecting:
       
   255         case EEmailAgentConnTerminated:
       
   256             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::ChangeNextStateL() StartWaitTimerL");
       
   257             if ( IsReconnectAfterError() )
       
   258                 {
       
   259                 StartWaitTimerL();
       
   260                 }
       
   261             else
       
   262                 {
       
   263                 DisconnectL( ETrue );
       
   264                 // disconnect if we are connected
       
   265                 iState = EEmailAgentIdle;
       
   266                 //give up, reset retry counter
       
   267                 iRetryCounter = 0;
       
   268                 CreateCompletedOpL();
       
   269                 }
       
   270             break;
       
   271         case EEmailAgentTimerWaiting:
       
   272             //just make sure that wait has completed before connecting
       
   273             ConnectIfAllowedL( iIntervalWaitId );
       
   274             break;
       
   275 
       
   276         case EEmailAgentTimerWaitingForStart://wait end, can connect
       
   277             ConnectIfAllowedL( iWaitForStartOpId );
       
   278             //else, do nothing, we are not going to change state until waitForStart timer completes
       
   279             break;
       
   280         case EEmailAgentIdle:
       
   281             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::ChangeNextStateL() Idling.... zZzZzZzZzzzzzzzzzzzzz");
       
   282             //No handling for idle, we just "hang around"
       
   283             break;
       
   284         case EEmailAgentFatalError:
       
   285             //something is so wrong that connection cannot be made without user intervention
       
   286             //like incorrect settings or such
       
   287             DisplayGlobalErrorNoteL();
       
   288             SwitchAutoUpdateOffL();
       
   289             break;
       
   290         default:
       
   291             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::ChangeNextStateL() Unknown state!: %d,resetting all!", iState);
       
   292             ResetAll();
       
   293             iState = EEmailAgentInitialised;
       
   294             CreateCompletedOpL();
       
   295             break;
       
   296         }
       
   297     }
       
   298 
       
   299 // ----------------------------------------------------------------------------
       
   300 // StartWaitTimerL
       
   301 // ----------------------------------------------------------------------------
       
   302 void CAlwaysOnlinePop3Agent::StartWaitTimerL( )
       
   303     {
       
   304     AOLOG_IN( "CAlwaysOnlinePop3Agent::StartWaitTimerL" );
       
   305     //waiting already
       
   306     if ( iState == EEmailAgentTimerWaiting )
       
   307         {
       
   308         KAOEMAIL_LOGGER_FN1("CAlwaysOnlinePop3Agent::StartWaitTimerL(), already waiting");
       
   309         return;
       
   310         }
       
   311     
       
   312     TTime time;
       
   313     time.HomeTime();
       
   314 
       
   315     //if just exited settings we should connect, only when AO is on (not EMN).
       
   316     if ( iFlags->Flag( EAOBFConnectNow ) && !IsEmn() )
       
   317         {
       
   318         // Set to false, one time only
       
   319         iFlags->ClearFlag( EAOBFConnectNow );
       
   320         
       
   321         TTimeIntervalSeconds conn = KInitWaitSeconds;
       
   322         time += conn;
       
   323 
       
   324         StartTimerOperationL( time, iIntervalWaitId );
       
   325         iState = EEmailAgentTimerWaiting;
       
   326         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartWaitTimerL. Connecting in 5 seconds. Op Id:  %d ",iIntervalWaitId );
       
   327         return;
       
   328         }
       
   329 
       
   330     //if we are retrying a connection. 3 quick retries and then qive up
       
   331     if ( (iRetryCounter % KAOMaxRetries) != 0 )
       
   332         {
       
   333         TTimeIntervalSeconds seconds( 0 );
       
   334         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartWaitTimerL() iRetryCounter %d", iRetryCounter);
       
   335 
       
   336         switch ( (iRetryCounter % KAOMaxRetries) )
       
   337             {
       
   338             //values for these retry times configured in AlwaysOnlineEmailAgentBase.h
       
   339             case 1:
       
   340             case 2: // CSI: 47 # see comment above
       
   341                 seconds = EEMailAgentReconnectThree;
       
   342                 break;
       
   343             default:
       
   344                 seconds = EEMailAgentReconnectFive;
       
   345                 break;
       
   346             }//switch
       
   347 
       
   348         time += seconds;
       
   349         }//if
       
   350     else if( iState == EEmailAgentQueued )
       
   351         {
       
   352         TTimeIntervalSeconds seconds( 0 );
       
   353         seconds = EEMailAgentReconnectFour;
       
   354         time += seconds;
       
   355         }
       
   356     else if ( !IsEmn() )
       
   357         {
       
   358         TInt minutes = RetrievalIntervalInMinutes( LoadSettingL<TInt>( 
       
   359             TImumDaSettings::EKeyAutoRetrievalInterval, EFalse ));
       
   360         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartWaitTimerL(). Minutes to wait: %d", minutes);
       
   361 
       
   362         TTimeIntervalMinutes intervalMinutes( minutes );
       
   363         time += intervalMinutes;
       
   364         }
       
   365 
       
   366     StartTimerOperationL( time, iIntervalWaitId );
       
   367     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartWaitTimerL. Interval timer started. Op Id:  %d ",iIntervalWaitId );
       
   368 
       
   369     iState = EEmailAgentTimerWaiting;
       
   370     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::StartWaitTimerL. Setting state:  %d ",iState );
       
   371     }
       
   372 
       
   373 // ----------------------------------------------------------------------------
       
   374 // CreatePop3OperationL()
       
   375 // ----------------------------------------------------------------------------
       
   376 void CAlwaysOnlinePop3Agent::CreatePop3OperationL( 
       
   377     TInt aFunctionId, 
       
   378     TBool aCompletedOperation /*= EFalse*/ )
       
   379     {
       
   380     AOLOG_IN( "CAlwaysOnlinePop3Agent::CreatePop3OperationL" );
       
   381     TMsvOp dummy;
       
   382     CreatePop3OperationL( dummy, aFunctionId, aCompletedOperation );
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // CAlwaysOnlinePop3Agent::FillMailOptionsL()
       
   387 // ---------------------------------------------------------------------------
       
   388 //
       
   389 TImPop3PopulateOptions CAlwaysOnlinePop3Agent::FillMailOptionsL()
       
   390     {
       
   391     AOLOG_IN( "CAlwaysOnlinePop3Agent::FillMailOptionsL" );
       
   392     TImPop3PopulateOptions pop3GetMailInfo;
       
   393 
       
   394     TInt32 sync = LoadSettingL<TInt32>( 
       
   395         TImumInSettings::EKeySyncFlags, ETrue );
       
   396 
       
   397     if ( sync == TImumInSettings::EFlagDownloadPartialCumulative )
       
   398         {
       
   399         // Population limit is set only if user defined limit has been defined
       
   400         TInt32 limit = LoadSettingL<TInt32>(
       
   401             TImumInSettings::EKeyDownloadBodySize, ETrue );
       
   402         pop3GetMailInfo.SetPopulationLimit(
       
   403             ( limit * KEmailKB ) / KEmailMaxCharsInLine );
       
   404         }
       
   405         
       
   406     return pop3GetMailInfo;
       
   407     }
       
   408 
       
   409 // ----------------------------------------------------------------------------
       
   410 // CreatePop3OperationL
       
   411 // ----------------------------------------------------------------------------
       
   412 void CAlwaysOnlinePop3Agent::CreatePop3OperationL( 
       
   413     TMsvOp& aOpId, 
       
   414     TInt aFunctionId, 
       
   415     TBool aCompletedOperation /*= EFalse*/ )
       
   416     {
       
   417     AOLOG_IN( "CAlwaysOnlinePop3Agent::CreatePop3OperationL" );
       
   418     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::CreatePop3OperationL() aFunctionId %d", aFunctionId);
       
   419 
       
   420     CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   421     CleanupStack::PushL( selection );
       
   422     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL(*this);
       
   423     CleanupStack::PushL( watcher );
       
   424     CMsvOperation* op = NULL;
       
   425 
       
   426     if ( aCompletedOperation )
       
   427         {
       
   428         op = CMsvCompletedOperation::NewL(  // CSI: 35 # cleanupstack is used
       
   429                 iSession, iEntry->Entry().iMtm, KNullDesC8, 
       
   430                 KMsvLocalServiceIndexEntryId, 
       
   431                 watcher->iStatus, KErrCancel);
       
   432 
       
   433         aOpId = op->Id();
       
   434 
       
   435         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::CreatePop3OperationL: ADDING COMPLETED OPERATION. ID: %d", aOpId );
       
   436         }
       
   437     else
       
   438         {
       
   439         selection->AppendL( iEntry->Entry().iServiceId );
       
   440         iPop3ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   441 
       
   442         TImPop3PopulateOptions pop3GetMailInfo = FillMailOptionsL();
       
   443         // Add the population limit to packet for fetch operation
       
   444         TPckg<TImPop3PopulateOptions> params(pop3GetMailInfo);
       
   445         
       
   446         // Begin the fetching operation
       
   447         op = iPop3ClientMtm->InvokeAsyncFunctionL( 
       
   448             aFunctionId, *selection, params, watcher->iStatus);
       
   449 
       
   450         aOpId = op->Id();
       
   451         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::CreatePop3OperationL: ADDING CONNECT OPERATION. ID: %d", aOpId );
       
   452         }
       
   453 
       
   454     CleanupStack::PushL( op );
       
   455     AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
   456     CleanupStack::Pop( 2, watcher); // op // CSI: 12,47 # nothing wrong
       
   457     CleanupStack::PopAndDestroy( selection );//selection
       
   458     KAOEMAIL_LOGGER_FN2("CAlwaysOnlinePop3Agent::CreatePop3OperationL");
       
   459     }
       
   460 
       
   461 
       
   462 // ----------------------------------------------------------------------------
       
   463 // DoFilteredPopulateL
       
   464 // ----------------------------------------------------------------------------
       
   465 void CAlwaysOnlinePop3Agent::DoFilteredPopulateL()
       
   466     {
       
   467     AOLOG_IN( "CAlwaysOnlinePop3Agent::DoFilteredPopulateL" );
       
   468     TInt32 sync = LoadSettingL<TInt32>( 
       
   469         TImumInSettings::EKeySyncFlags, ETrue );
       
   470 
       
   471     if ( sync == TImumInSettings::EFlagDownloadPartialCumulative ||
       
   472          sync == TImumInSettings::EFlagSetDownloadAll )
       
   473         {
       
   474         // Either user defined limit or full download is selected
       
   475         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::DoFilteredPopulateL(): Doing filtered populate with setting: %d", sync );
       
   476         iState = EEmailAgentDoingFilteredPopulate;
       
   477         CreatePop3OperationL( 
       
   478             iFilteredPopulateOpId, 
       
   479             KPOP3MTMPopulateNew,
       
   480             EFalse );
       
   481         }
       
   482     else
       
   483         {
       
   484         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::DoFilteredPopulateL(): Filters not set. Will not populate." );
       
   485         //set state and do completed op to step to next state
       
   486         iState = EEmailAgentDoingFilteredPopulate;
       
   487         CreateCompletedOpL();
       
   488         }
       
   489     }
       
   490 
       
   491 // ----------------------------------------------------------------------------
       
   492 // HandleOpErrorL
       
   493 // ----------------------------------------------------------------------------
       
   494 void CAlwaysOnlinePop3Agent::HandleOpErrorL()
       
   495     {
       
   496     AOLOG_IN( "CAlwaysOnlinePop3Agent::HandleOpErrorL" );
       
   497     AOLOG_WRMV( "iError: ",  EAoNormalError, iError, EAoLogSt3 );
       
   498     KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::HandleOpErrorL() Error Handling started" );
       
   499     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::HandleOpErrorL() Error ID: %d", iError );
       
   500     
       
   501     switch ( iError )
       
   502         {
       
   503         case KErrNone:
       
   504             //shouldn't come here if KErrNone
       
   505             break;
       
   506         case KErrAbort:
       
   507         case KErrCancel:
       
   508             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::HandleOpErrorL(): KErrCancel");
       
   509             //sometimes we seem to get these errors with connection operations.
       
   510             //so go to initialised state -> reset all
       
   511             iError = KErrNone;
       
   512             iState = EEmailAgentInitialised; //this will be changed where those timers were cancelled.
       
   513             break;
       
   514         
       
   515         case KErrGeneral://seems that this is what is given when phone/csd is in use
       
   516             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::HandleOpErrorL(): KErrGeneral");
       
   517             iState = EEmailAgentQueued;
       
   518             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   519             break;
       
   520         case KErrInUse:
       
   521             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::HandleOpErrorL(): KErrInUse");
       
   522             iState = EEmailAgentQueued;
       
   523             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   524             break;
       
   525         //fatal errors, cannot recover without user intervention
       
   526         case KPop3InvalidUser:
       
   527         case KPop3InvalidLogin:
       
   528         case KPop3InvalidApopLogin:
       
   529         case KErrPop3TLSNegotiateFailed:
       
   530         case KImskSSLTLSNegotiateFailed:
       
   531         case KErrCorrupt:
       
   532             {
       
   533             iState = EEmailAgentFatalError;
       
   534             iRetryCounter = 0;
       
   535             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlinePop3Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   536             }
       
   537             break;
       
   538         //This error occurs sometimes when network is reserverd or there is a dns problems
       
   539         case KErrAoServerNotFound:
       
   540             HandleDefaultError();
       
   541             break;
       
   542             
       
   543         case KErrConnectionTerminated://red key
       
   544         case KErrTimedOut:
       
   545             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::HandleOpErrorL. KErrConnectionTimedOut(-33) OR KErrConnectionTerminated");
       
   546             iState = EEmailAgentConnTerminated;
       
   547             break;
       
   548         //let these go to default retry mechanism
       
   549         case KImskErrorDNSNotFound:
       
   550         case KImskErrorControlPanelLocked:
       
   551         case KImskErrorISPOrIAPRecordNotFound:
       
   552         case KImskErrorActiveSettingIsDifferent:
       
   553         case KImskSecuritySettingsFailed:
       
   554         case KPop3CannotCreateApopLogonString:            
       
   555         case KPop3ProblemWithRemotePopServer:            
       
   556         case KPop3CannotOpenServiceEntry:            
       
   557         case KPop3CannotSetRequiredFolderContext:
       
   558         case KPop3CannotConnect:
       
   559         default:
       
   560             HandleDefaultError();
       
   561             break;
       
   562         };
       
   563     //reset error
       
   564     iError = KErrNone;
       
   565     }
       
   566 
       
   567 // ----------------------------------------------------------------------------
       
   568 // DoSyncDisconnectL
       
   569 // ----------------------------------------------------------------------------
       
   570 void CAlwaysOnlinePop3Agent::DoSyncDisconnectL()
       
   571     {
       
   572     AOLOG_IN( "CAlwaysOnlinePop3Agent::DoSyncDisconnectL" );
       
   573     if ( iEntry->Entry().Connected() )
       
   574         {
       
   575         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::DoSyncDisconnectL() Doing SyncDisconnect! ");
       
   576 
       
   577     	TBuf8<1> dummyParams;
       
   578     	dummyParams.Zero();
       
   579 
       
   580 		// Needed to change next operation to synchronized
       
   581         CMsvOperationActiveSchedulerWait* wait = 
       
   582         	CMsvOperationActiveSchedulerWait::NewLC();
       
   583         
       
   584         CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   585         CleanupStack::PushL( selection );
       
   586 
       
   587         selection->AppendL( iEntry->Entry().iServiceId );
       
   588         iPop3ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   589         CMsvOperation* op = iPop3ClientMtm->InvokeAsyncFunctionL( 
       
   590             KPOP3MTMDisconnect, *selection, dummyParams, wait->iStatus);
       
   591 
       
   592 		// Start wait object
       
   593         wait->Start();
       
   594         
       
   595         delete op;
       
   596         CleanupStack::PopAndDestroy( 2, wait ); // selection // CSI: 12,47 # nothing wrong
       
   597 
       
   598         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlinePop3Agent::DoSyncDisconnectL() Should be disconnected now! ");
       
   599         }
       
   600     }
       
   601 
       
   602 // ----------------------------------------------------------------------------
       
   603 //  HandleOpCompleted
       
   604 // ----------------------------------------------------------------------------
       
   605 void CAlwaysOnlinePop3Agent::HandleOpCompleted( 
       
   606     TMsvOp opId, 
       
   607     TInt aCompletionCode )
       
   608     {
       
   609     AOLOG_IN( "CAlwaysOnlinePop3Agent::HandleOpCompleted" );
       
   610     TInt trapErr;
       
   611     
       
   612     //save error code so we can handle errors elsewhere
       
   613     if ( opId == iConnectOpId && aCompletionCode != KErrNone )
       
   614         {
       
   615         iError = aCompletionCode;
       
   616         SetLastUpdateFailed();
       
   617         }
       
   618     else if ( opId == iConnectOpId )
       
   619         {
       
   620         iError = KErrNone;
       
   621         iConnectOpId = KErrNotFound;
       
   622         iRetryCounter = 0; //reset retry counter
       
   623 
       
   624         //set last successful update date and time to settings
       
   625         SetLastSuccessfulUpdate();
       
   626         
       
   627         }
       
   628     else if ( opId == iFilteredPopulateOpId )
       
   629         {
       
   630         iFilteredPopulateOpId = KErrNotFound;
       
   631         }
       
   632     else if ( opId == iIntervalWaitId )
       
   633         {
       
   634         iIntervalWaitId = KErrNotFound;
       
   635         }
       
   636     else if ( opId == iWaitForStopOpId )
       
   637         {
       
   638         if ( aCompletionCode == KErrNone )
       
   639             {
       
   640             iWaitForStopOpId = KErrNotFound;
       
   641             TRAP( trapErr, CheckAndHandleSchedulingL() );
       
   642             }
       
   643         }
       
   644     else if ( opId == iWaitForStartOpId )
       
   645         {
       
   646         if ( aCompletionCode == KErrNone )
       
   647             {
       
   648             iWaitForStartOpId = KErrNotFound;
       
   649             TRAP( trapErr, CheckAndHandleSchedulingL() );
       
   650             }
       
   651         }
       
   652     else if ( opId == iStartDelayOpId )
       
   653         {
       
   654         iFlags->SetFlag( EAOBFConnectNow );        
       
   655         //leave id untouched. We need this only once, on bootup, or when agent is activated via settings
       
   656         //we use this to check is it used already
       
   657         //iStartDelayOpId;
       
   658         }
       
   659     
       
   660     TRAP_IGNORE( CallNewMessagesL() );
       
   661     }
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 //  CreateCompletedOpL
       
   665 // ----------------------------------------------------------------------------
       
   666 void CAlwaysOnlinePop3Agent::CreateCompletedOpL()
       
   667     {
       
   668     AOLOG_IN( "CAlwaysOnlinePop3Agent::CreateCompletedOpL" );
       
   669     CreatePop3OperationL( 0, ETrue );//creates completed operation
       
   670     }
       
   671 
       
   672 // ----------------------------------------------------------------------------
       
   673 //  MailHasBeenSyncedL()
       
   674 // ----------------------------------------------------------------------------
       
   675 TBool CAlwaysOnlinePop3Agent::MailHasBeenSyncedL()
       
   676     {
       
   677     AOLOG_IN( "CAlwaysOnlinePop3Agent::MailHasBeenSyncedL" );
       
   678 
       
   679     //we need to set this only once during the lifetime of ImapAgent
       
   680     if ( iHasBeenSynced )
       
   681         {
       
   682         return iHasBeenSynced;
       
   683         }
       
   684 
       
   685     iHasBeenSynced = EFalse;
       
   686     
       
   687     CMsvEntrySelection* sel = iEntry->ChildrenL();
       
   688     CleanupStack::PushL( sel );
       
   689     if ( sel->Count() )
       
   690         {
       
   691         iHasBeenSynced = ETrue;
       
   692         }
       
   693     CleanupStack::PopAndDestroy( sel );
       
   694 
       
   695     return iHasBeenSynced;
       
   696     }