email/alwaysonlineemailplugin/src/AlwaysOnlineImapAgent.cpp
changeset 79 2981cb3aa489
parent 0 72b543305e3a
equal deleted inserted replaced
25:84d9eb65b26f 79:2981cb3aa489
       
     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 *     Email imap agent
       
    16 *
       
    17 */
       
    18 
       
    19 #include <AlwaysOnlineStatusQueryInterface.h>
       
    20 #include <SenduiMtmUids.h>
       
    21 #include <es_sock.h>
       
    22 #include <MuiuOperationWait.h>
       
    23 #include <ImumInSettingsData.h>
       
    24 
       
    25 #include "AlwaysOnlineEmailPluginTimer.h"
       
    26 #include "AlwaysOnlineEmailPluginLogging.h"
       
    27 #include "AlwaysOnlineEmailLoggingTools.h"
       
    28 #include "AlwaysOnlineImapAgent.h"
       
    29 #include "AlwaysOnlineImap4FolderPopulate.h"
       
    30 
       
    31 const TInt KAlwaysOnlineMaxLimit = 30;  // if inbox sync rate is less than this and we are using gprs, then connection is kept open all the time.
       
    32 const TInt KAlwaysOnlineTwoMinutes = 120;
       
    33 const TInt KAlwaysOnlineMinuteToSecondsMultiplier = 60;
       
    34 
       
    35 
       
    36 // ----------------------------------------------------------------------------
       
    37 // CAlwaysOnlineImap4Agent()
       
    38 // ----------------------------------------------------------------------------
       
    39 CAlwaysOnlineImap4Agent::CAlwaysOnlineImap4Agent(
       
    40     CMsvSession& aSession,
       
    41     CClientMtmRegistry& aClientMtmRegistry,
       
    42     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager,
       
    43     CAlwaysOnlineEmailAgent& aEmailAgent )
       
    44     :
       
    45     CAlwaysOnlineEmailAgentBase(
       
    46         aSession,
       
    47         aClientMtmRegistry,
       
    48         aAlwaysOnlineManager,
       
    49         aEmailAgent ),
       
    50     iInboxId( 0 ),
       
    51     iImap4StateFlags()
       
    52     {
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // NewL
       
    57 // ----------------------------------------------------------------------------
       
    58 CAlwaysOnlineImap4Agent* CAlwaysOnlineImap4Agent::NewL(
       
    59     CMsvSession& aSession,
       
    60     CClientMtmRegistry& aClientMtmRegistry,
       
    61     TMsvId aMailboxId,
       
    62     MAlwaysOnlineStatusQueryInterface& aAlwaysOnlineManager,
       
    63     CAlwaysOnlineEmailAgent& aEmailAgent )
       
    64     {
       
    65     AOLOG_IN( "CAlwaysOnlineImap4Agent::IsReconnectAfterError" );
       
    66     CAlwaysOnlineImap4Agent* self = new(ELeave)CAlwaysOnlineImap4Agent(
       
    67         aSession,
       
    68         aClientMtmRegistry,
       
    69         aAlwaysOnlineManager,
       
    70         aEmailAgent );
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL( aMailboxId );
       
    73     CleanupStack::Pop( self );
       
    74     return self;
       
    75     }
       
    76 
       
    77 // ----------------------------------------------------------------------------
       
    78 // ConstructL()
       
    79 // ----------------------------------------------------------------------------
       
    80 void CAlwaysOnlineImap4Agent::ConstructL( TMsvId aMailboxId )
       
    81     {
       
    82     AOLOG_IN( "CAlwaysOnlineImap4Agent::ConstructL" );
       
    83     CAlwaysOnlineEmailAgentBase::ConstructL( aMailboxId );
       
    84     iImap4ClientMtm = (CImap4ClientMtm*) iClientMtmRegistry.NewMtmL(
       
    85         KSenduiMtmImap4Uid );
       
    86     iFolderArray = new (ELeave) CMsvEntrySelection();
       
    87     iFlags->SetFlag( EAOBFIsImap4 );
       
    88     }
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // ~CAlwaysOnlineImap4Agent()
       
    92 // ----------------------------------------------------------------------------
       
    93 CAlwaysOnlineImap4Agent::~CAlwaysOnlineImap4Agent()
       
    94     {
       
    95     AOLOG_IN( "CAlwaysOnlineImap4Agent::~CAlwaysOnlineImap4Agent" );
       
    96     // cancel ongoing operations
       
    97     CloseServices();
       
    98     delete iFolderArray;
       
    99     delete iFolderObserver;
       
   100     delete iImap4ClientMtm;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 // SetStayOnlineFlagL()
       
   105 // ----------------------------------------------------------------------------
       
   106 void CAlwaysOnlineImap4Agent::SetStayOnlineFlagL()
       
   107     {
       
   108     AOLOG_IN( "CAlwaysOnlineImap4Agent::SetStayOnlineFlagL" );
       
   109     //first, check the mode we should be operating in.
       
   110     TBool isCsd = EFalse;
       
   111     TRAP_IGNORE( isCsd = IsBearerCSDL() );
       
   112 
       
   113     TInt inboxTime = RetrievalIntervalInMinutes( LoadSettingL<TInt>( 
       
   114             TImumDaSettings::EKeyAutoRetrievalInterval, EFalse ));
       
   115     if ( inboxTime <= KAlwaysOnlineMaxLimit && !isCsd )
       
   116         {
       
   117         iImap4StateFlags.SetFlag( EAlwaysOnlineImap4StayOnline );
       
   118         }
       
   119     else
       
   120         {
       
   121         iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4StayOnline );
       
   122         }
       
   123     }
       
   124 
       
   125 // ----------------------------------------------------------------------------
       
   126 // StartL()
       
   127 // ----------------------------------------------------------------------------
       
   128 void CAlwaysOnlineImap4Agent::StartL()
       
   129     {
       
   130     AOLOG_IN( "CAlwaysOnlineImap4Agent::StartL" );
       
   131     //once start delay op has been started we don't ever reset that id.
       
   132     if ( iStartDelayOpId == KErrNotFound )
       
   133         {
       
   134         //check offline & roaming statuses from AO server
       
   135         TBool suspended = EFalse;
       
   136         QueryAndHandleAOServerInfoL( suspended );
       
   137 
       
   138         if ( suspended )
       
   139             {
       
   140             //we got iState = EEmailAgentIdle from CAlwaysOnlineEmailAgentBase::Suspend
       
   141             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::StartL() We have been suspended, not starting!");
       
   142             return;
       
   143             }
       
   144 
       
   145         iState = EEmailAgentInitialised;
       
   146         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartL. Setting state: %d ",iState );
       
   147 
       
   148 
       
   149         TTime home;
       
   150         home.HomeTime();
       
   151 
       
   152         //Let's not start immediately on start. Small delay is safer
       
   153         TTimeIntervalSeconds startDelay( KInitWaitSeconds );
       
   154         home += startDelay;
       
   155 
       
   156         StartTimerOperationL( home, iStartDelayOpId );
       
   157         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartL(). Activated startDelayWait 5sec Op Id: %d ",iStartDelayOpId );
       
   158         }//if
       
   159     else
       
   160         {
       
   161         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::StartL() called, but we're allready started. No changes to state machine");
       
   162         }
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // ResumeL()
       
   167 // ----------------------------------------------------------------------------
       
   168 void CAlwaysOnlineImap4Agent::ResumeL( const TBool aConnectNow )
       
   169     {
       
   170     AOLOG_IN( "CAlwaysOnlineImap4Agent::ResumeL" );
       
   171     CAlwaysOnlineEmailAgentBase::ResumeL( aConnectNow );
       
   172 
       
   173 	// just make sure that connection will not stay open
       
   174 	iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4StayOnline );
       
   175     }
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 // CreateImap4OperationL()
       
   179 // ----------------------------------------------------------------------------
       
   180 void CAlwaysOnlineImap4Agent::CreateImap4OperationL(
       
   181     TInt aFunctionId,
       
   182     TBool aCompletedOperation /*= EFalse*/ )
       
   183     {
       
   184     AOLOG_IN( "CAlwaysOnlineImap4Agent::CreateImap4OperationL" );
       
   185     TMsvOp dummy;
       
   186     CreateImap4OperationL( dummy, aFunctionId, aCompletedOperation );
       
   187     }
       
   188 
       
   189 // ----------------------------------------------------------------------------
       
   190 // CreateImap4OperationL()
       
   191 // ----------------------------------------------------------------------------
       
   192 void CAlwaysOnlineImap4Agent::CreateImap4OperationL(
       
   193     TMsvOp& aOpId,
       
   194     TInt aFunctionId,
       
   195     TBool aCompletedOperation /*= EFalse*/ )
       
   196     {
       
   197     AOLOG_IN( "CAlwaysOnlineImap4Agent::CreateImap4OperationL" );
       
   198     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::CreateImap4OperationL() aFunctionId %d", aFunctionId);
       
   199 
       
   200     CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   201     CleanupStack::PushL( selection );
       
   202     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL( *this );
       
   203     CleanupStack::PushL( watcher );
       
   204     CMsvOperation* op = NULL;
       
   205 
       
   206     if ( aCompletedOperation )
       
   207         {
       
   208         op = CMsvCompletedOperation::NewL(  // CSI: 35 # cleanupstack is used
       
   209                 iSession, iEntry->Entry().iMtm, KNullDesC8,
       
   210                 KMsvLocalServiceIndexEntryId,
       
   211                 watcher->iStatus, KErrCancel);
       
   212 
       
   213         aOpId = op->Id();
       
   214 
       
   215         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::CreateImap4OperationL: ADDING COMPLETED OPERATION. ID: %d", aOpId );
       
   216         }
       
   217     else
       
   218         {
       
   219         TPckg<MMsvImapConnectionObserver*> param( this );
       
   220 
       
   221         selection->AppendL( iEntry->Entry().iServiceId );
       
   222         iImap4ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   223 
       
   224         op = iImap4ClientMtm->InvokeAsyncFunctionL(
       
   225             aFunctionId, *selection, param, watcher->iStatus);
       
   226 
       
   227         aOpId = op->Id();
       
   228         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::CreateImap4OperationL: ADDING CONNECT OPERATION. ID: %d", aOpId );
       
   229 
       
   230         }
       
   231     CleanupStack::PushL( op );
       
   232     AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
   233     CleanupStack::Pop( 2, watcher ); // op // CSI: 12,47 # nothing wrong
       
   234     CleanupStack::PopAndDestroy( selection );//selection
       
   235     }
       
   236 
       
   237 
       
   238 // ----------------------------------------------------------------------------
       
   239 // ConnectAndUpdateHeadersL()
       
   240 // ----------------------------------------------------------------------------
       
   241 void CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL()
       
   242     {
       
   243     AOLOG_IN( "CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL" );
       
   244     KAOEMAIL_LOGGER_WRITE_FORMAT( "CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL(). Starting to connect into: %d", iEntry->Entry().Id() );
       
   245 
       
   246     //Just connect. Whether to stay online or just poll, is decided elsewhere
       
   247     if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) && !IsEmn() )
       
   248         {
       
   249         KAOEMAIL_LOGGER_WRITE(" CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL() Connecting to stay online");
       
   250         //do full sync, inbox and folders
       
   251         CreateImap4OperationL( iConnectAndStayOnlineOpId,
       
   252             KIMAP4MTMConnectAndSyncCompleteAfterDisconnect,
       
   253             EFalse );
       
   254         iState = EEmailAgentConnectingToStayOnline;
       
   255 
       
   256         //folder sync is started from HandleImapConnectionEvent after sync completes
       
   257         //mail partial fetch is also started from HandleImapConnectionEvent
       
   258         }
       
   259     else
       
   260         {
       
   261         KAOEMAIL_LOGGER_WRITE(" CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL() Doing plaing connect");
       
   262         //just connect. After this completes, sync is invoked elsewhere
       
   263         CreateImap4OperationL( iConnectOpId, KIMAP4MTMConnect, EFalse );
       
   264         iState = EEmailAgentPlainConnecting;
       
   265         }
       
   266 
       
   267 
       
   268     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::ConnectAndUpdateHeadersL(). Setting state:  %d ",iState );
       
   269     }
       
   270 
       
   271 // ----------------------------------------------------------------------------
       
   272 // DisconnectL()
       
   273 // ----------------------------------------------------------------------------
       
   274 void CAlwaysOnlineImap4Agent::DisconnectL( TBool aAutomatic )
       
   275     {
       
   276     AOLOG_IN( "CAlwaysOnlineImap4Agent::DisconnectL" );
       
   277     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::DisconnectL");
       
   278 
       
   279     TBuf8<1> dummyParams;
       
   280     dummyParams.Zero();
       
   281     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL( *this );
       
   282     CleanupStack::PushL( watcher );
       
   283     CMsvOperation* op = NULL;
       
   284 
       
   285     if ( iEntry->Entry().Connected() )
       
   286         {
       
   287         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DisconnectL() Connected");
       
   288         CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   289         CleanupStack::PushL( selection );
       
   290         selection->AppendL( iEntry->Entry().iServiceId );
       
   291         iImap4ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   292         op = iImap4ClientMtm->InvokeAsyncFunctionL(
       
   293             KIMAP4MTMDisconnect, *selection, dummyParams, watcher->iStatus);
       
   294 
       
   295         iDisconnectOpId = op->Id();
       
   296         CleanupStack::PopAndDestroy( selection );//selection
       
   297         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::DisconnectL: ADDING DISCONNECT OPERATION: %d", iDisconnectOpId );
       
   298         }
       
   299     else
       
   300         {
       
   301         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DisconnectL() Not connected");
       
   302         //already disconnected
       
   303         op = CMsvCompletedOperation::NewL(
       
   304                 iSession, iEntry->Entry().iMtm, KNullDesC8,
       
   305                 KMsvLocalServiceIndexEntryId,
       
   306                 watcher->iStatus, KErrCancel);
       
   307         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::DisconnectL: ADDING COMPLETED OPERATION: %d", op->Id() );
       
   308         }
       
   309 
       
   310     CleanupStack::PushL( op );
       
   311     AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
   312     CleanupStack::Pop( 2, watcher); // op // CSI: 12,47 # nothing wrong
       
   313 
       
   314     if ( aAutomatic )
       
   315         {
       
   316         iState = EEmailAgentAutoDisconnecting;
       
   317         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::DisconnectL. Setting state:  %d ",iState );
       
   318         }
       
   319     else
       
   320         {
       
   321         iState = EEmailAgentUserDisconnecting;
       
   322         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::DisconnectL. Setting state:  %d ",iState );
       
   323         }
       
   324     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::DisconnectL");
       
   325     }
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // SynchroniseNewL()
       
   329 // ----------------------------------------------------------------------------
       
   330 void CAlwaysOnlineImap4Agent::SynchroniseNewL()
       
   331     {
       
   332     AOLOG_IN( "CAlwaysOnlineImap4Agent::SynchroniseNewL" );
       
   333     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::SynchroniseNewL");
       
   334     TBuf8<1> dummyParams;
       
   335     dummyParams.Zero();
       
   336 
       
   337     CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL(*this);
       
   338     CleanupStack::PushL( watcher );
       
   339     CMsvOperation* op = NULL;
       
   340 
       
   341     //we must be connected to use inboxnewsync
       
   342     if ( iEntry->Entry().Connected() )
       
   343         {
       
   344         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::SynchroniseNewL() Connected");
       
   345         SetLastSuccessfulUpdate();
       
   346         CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   347         CleanupStack::PushL( selection );
       
   348         selection->AppendL( iEntry->Entry().iServiceId );
       
   349         iImap4ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   350         op = iImap4ClientMtm->InvokeAsyncFunctionL(
       
   351             KIMAP4MTMFullSync, *selection, dummyParams, watcher->iStatus);
       
   352 
       
   353         iSyncOpId = op->Id();
       
   354 
       
   355         CleanupStack::PopAndDestroy( selection );//selection
       
   356         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::SynchroniseNewL: Adding synchronise new operation. ID: %d", iSyncOpId );
       
   357 
       
   358         iState = EEmailAgentSynchronising;
       
   359         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::SynchroniseNewL. Setting state:  %d ",iState );
       
   360         }
       
   361     else
       
   362         {
       
   363         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::SynchroniseNewL() Not connected");
       
   364         //for some reason we have been disconnected before we got to sync part. Invoke normal retry procedure
       
   365         iError = KErrDisconnected; //this error will cause us to retry
       
   366         op = CMsvCompletedOperation::NewL(
       
   367                 iSession, iEntry->Entry().iMtm, KNullDesC8,
       
   368                 KMsvLocalServiceIndexEntryId,
       
   369                 watcher->iStatus, KErrCancel);
       
   370         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::SynchroniseNewL: Connection was dropped, retry!" );
       
   371         }
       
   372 
       
   373     CleanupStack::PushL( op );
       
   374     AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
   375     CleanupStack::Pop( 2, watcher); // op // CSI: 12,47 # nothing wrong
       
   376     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::SynchroniseNewL");
       
   377     }
       
   378 
       
   379 // ----------------------------------------------------------------------------
       
   380 // HandleImapConnectionEvent() from MMsvImapConnectionObserver
       
   381 // ----------------------------------------------------------------------------
       
   382 void CAlwaysOnlineImap4Agent::HandleImapConnectionEvent(
       
   383     TImapConnectionEvent aConnectionEvent )
       
   384     {
       
   385     AOLOG_IN( "CAlwaysOnlineImap4Agent::HandleImapConnectionEvent" );
       
   386     TInt trapErr = KErrNone;
       
   387     switch ( aConnectionEvent )
       
   388         {
       
   389         case ESynchronisationComplete:
       
   390             //we can reset retrycounter when sync has occurred. Because it can be
       
   391             //taken as success, so it would not be retrying for same connection anymore.
       
   392             iRetryCounter = 0;
       
   393 
       
   394             //this event can be used also to set last successful update
       
   395             SetLastSuccessfulUpdate();
       
   396 
       
   397             if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) /*iStayOnline*/ )
       
   398                 {
       
   399                 if ( iFilteredPopulateOpId == KErrNotFound )
       
   400                     {
       
   401                     // start fetch new mail operation
       
   402                     TRAP( trapErr, FetchNewMailL() );
       
   403                     if ( trapErr != KErrNone )
       
   404                         {
       
   405                         // try to start folder sync timer not the folder sync itself, because
       
   406                         // they were synced on connect already
       
   407                         TRAP( trapErr, StartFolderSyncTimerL() );
       
   408                         }
       
   409                     }
       
   410                 }
       
   411             break;
       
   412         case EDisconnecting:
       
   413             break;
       
   414         case EConnectionCompleted:
       
   415             break;
       
   416         default:
       
   417             break;
       
   418         }
       
   419     TRAP_IGNORE( CallNewMessagesL() );
       
   420     }
       
   421 
       
   422 
       
   423 // ----------------------------------------------------------------------------
       
   424 // ChangeNextStateL
       
   425 // ----------------------------------------------------------------------------
       
   426 void CAlwaysOnlineImap4Agent::ChangeNextStateL()
       
   427     {
       
   428     AOLOG_IN( "CAlwaysOnlineImap4Agent::ChangeNextStateL" );
       
   429     AOLOG_WRMV( "start iState: ", EAoMailPluginStates, iState, EAoLogSt3 );
       
   430     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::ChangeNextStateL() iState: %d", iState);
       
   431 
       
   432     if ( iState == EEmailAgentAutoDisconnecting &&
       
   433          iImap4StateFlags.Flag( EAlwaysOnlineImap4RemoveMeImmediately ) )
       
   434         {
       
   435         if ( !iImap4StateFlags.Flag( EAlwaysOnlineImap4DontRemoveOnDisconnect ) )
       
   436             {
       
   437             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::ChangeNextStateL() RemoveMeL 0x%x", iEntry->Entry().Id() );
       
   438             RemoveMe();
       
   439             return;
       
   440             }
       
   441         else
       
   442             {
       
   443             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::ChangeNextStateL() change 0x%x back to normal", iEntry->Entry().Id() );
       
   444             iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4DontRemoveOnDisconnect );
       
   445             iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4TemporaryConnectionObserver );
       
   446             iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4DontReconnect );
       
   447             iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4StayOnline );
       
   448             iImap4StateFlags.ClearFlag( EAlwaysOnlineImap4RemoveMeImmediately );
       
   449             }
       
   450         }
       
   451 
       
   452 
       
   453     //if we are waiting for schedule to start, we don't handle conn errors.
       
   454     //this should only occur if conn op has been started just before schedule ends.
       
   455     if ( iError != KErrNone && iState != EEmailAgentTimerWaitingForStart )
       
   456         {
       
   457         HandleOpErrorL();
       
   458         }
       
   459 
       
   460     switch ( iState )
       
   461         {
       
   462         // Agent just constructed ( or settings changed ) and ready. Should be
       
   463         // here only when settings have been saved with always online or mail
       
   464         // message notifications on.
       
   465         case EEmailAgentInitialised:
       
   466             //settings may have been changed, reset all pending operations
       
   467             //because their timings can be different compared to newly saved settings
       
   468 
       
   469             ResetAll();
       
   470 
       
   471             if ( !IsEmn() )
       
   472                 {
       
   473                 CheckAndHandleSchedulingL();//starts schedule timers and sets state accordingly
       
   474                 if ( iWaitForStopOpId != KErrNotFound ||
       
   475                      ( iWaitForStopOpId == KErrNotFound &&
       
   476                        iWaitForStartOpId == KErrNotFound ) )//no start or stop waiters, all times
       
   477                     {
       
   478                     //CheckAndHandleScheduling started waitForStopTimer
       
   479                     StartWaitTimerL();
       
   480                     }
       
   481                 //else, do nothing, we wait until waitForStartTimer expires and starts updates again
       
   482                 }
       
   483             else
       
   484                 {
       
   485                 iState = EEmailAgentIdle;
       
   486                 KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::ChangeNextStateL() EMN is on --> no scheduling or timers needed!");
       
   487                 }
       
   488             break;
       
   489 
       
   490         //case EEmailAgentConnected:
       
   491         case EEmailAgentConnecting:
       
   492             DisconnectL( ETrue );
       
   493             break;
       
   494         case EEmailAgentPlainConnecting:
       
   495             SynchroniseNewL();
       
   496             break;
       
   497         case EEmailAgentSynchronising:
       
   498             FetchNewMailL(); //check filtering options and acts accordingly
       
   499             break;
       
   500         case EEmailAgentFetching:
       
   501             if ( IsTemporary() || ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) &&
       
   502                 !IsEmn() ) )
       
   503                 {
       
   504                 StartFolderSyncTimerL();
       
   505                 }
       
   506             else
       
   507                 {
       
   508                 DisconnectL( ETrue );// Folder's not synchronized when EMN is on
       
   509                 }
       
   510             break;
       
   511         case EEmailAgentConnectingToStayOnline:
       
   512             //if this has completed, we have to connect again
       
   513             ConnectIfAllowedL( iConnectAndStayOnlineOpId );
       
   514             break;
       
   515         case EEmailAgentConnectFailed:
       
   516         case EEmailAgentQueued:
       
   517         case EEmailAgentReconnecting:
       
   518         case EEmailAgentAutoDisconnecting:
       
   519         case EEmailAgentUserDisconnecting:
       
   520         case EEmailAgentConnTerminated://red key
       
   521             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::ChangeNextStateL() StartWaitTimerL");
       
   522             if ( IsReconnectAfterError() )
       
   523                 {
       
   524                 StartWaitTimerL();
       
   525                 }
       
   526             else
       
   527                 {
       
   528                 DisconnectL( ETrue );// Folder's not synchronized when EMN is on
       
   529                 // disconnect if we are connected
       
   530                 iState = EEmailAgentIdle;
       
   531                 //give up, reset retry counter
       
   532                 iRetryCounter = 0;
       
   533                 CreateCompletedOpL();
       
   534                 }
       
   535             break;
       
   536         case EEmailAgentTimerWaiting:
       
   537             // Just make sure that wait has completed before connecting
       
   538             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::ChangeNextStateL() ConnectIfAllowedL");
       
   539             ConnectIfAllowedL( iIntervalWaitId );
       
   540             break;
       
   541 
       
   542         case EEmailAgentTimerWaitingForStart:
       
   543             // Wait end, can connect
       
   544             ConnectIfAllowedL( iWaitForStartOpId );
       
   545             // Else, do nothing, we are not going to change state until
       
   546             // waitForStart timer completes
       
   547             break;
       
   548         case EEmailAgentIdle:
       
   549             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::ChangeNextStateL() Idling.... zZzZzZzZzzzzzzzzzzzzz");
       
   550             // No handling for idle, we just "hang around"
       
   551             break;
       
   552         case EEmailAgentFatalError:
       
   553             // Something is so wrong that connection cannot be made
       
   554             // without user intervention like incorrect settings or such
       
   555             DisplayGlobalErrorNoteL();
       
   556             SwitchAutoUpdateOffL();
       
   557 
       
   558             break;
       
   559         default:
       
   560             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::ChangeNextStateL() Unknown state!: %d,resetting all!", iState);
       
   561             ResetAll();
       
   562             iState = EEmailAgentInitialised;
       
   563             CreateCompletedOpL();
       
   564             break;
       
   565         }
       
   566     AOLOG_WRMV( "end iState: ", EAoMailPluginStates, iState, EAoLogSt3 );
       
   567     }
       
   568 
       
   569 // ----------------------------------------------------------------------------
       
   570 // StartWaitTimerL
       
   571 // ----------------------------------------------------------------------------
       
   572 void CAlwaysOnlineImap4Agent::StartWaitTimerL()
       
   573     {
       
   574     AOLOG_IN( "CAlwaysOnlineImap4Agent::StartWaitTimerL" );
       
   575     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::StartWaitTimerL");
       
   576 
       
   577     if ( iIntervalWaitId > KErrNotFound )
       
   578         {
       
   579         KAOEMAIL_LOGGER_WRITE_FORMAT("already waiting, try to find wait operation: %d", iIntervalWaitId );
       
   580         const TInt count = iOperations.Count();
       
   581         for ( TInt i = count - 1; i >= 0; i-- )
       
   582             {
       
   583             CMsvOperation& oper = iOperations[i]->Operation();
       
   584             if ( oper.Id() == iIntervalWaitId )
       
   585                 {
       
   586                 CMsvSingleOpWatcher* opWatcher = iOperations[i];
       
   587                 KAOEMAIL_LOGGER_WRITE("wait operation found, delete it");
       
   588                 iOperations.Delete( i );
       
   589                 delete opWatcher;
       
   590                 iIntervalWaitId = KErrNotFound;
       
   591                 break;
       
   592                 }
       
   593             }
       
   594 
       
   595         }
       
   596 
       
   597     //waiting already
       
   598     if ( iState == EEmailAgentTimerWaiting )
       
   599         {
       
   600         KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StartWaitTimerL, already waiting");
       
   601         return;
       
   602         }
       
   603 
       
   604     TTime time;
       
   605     time.HomeTime();
       
   606 
       
   607     //if just exited settings we should connect, only when AO is on (not EMN).
       
   608     if ( iFlags->Flag( EAOBFConnectNow ) && !IsEmn() )
       
   609         {
       
   610         // Set to false, one time only
       
   611         iFlags->ClearFlag( EAOBFConnectNow );
       
   612         TTimeIntervalSeconds conn = KInitWaitSeconds;
       
   613         time += conn;
       
   614 
       
   615         StartTimerOperationL( time, iIntervalWaitId );
       
   616         iState = EEmailAgentTimerWaiting;
       
   617         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartWaitTimerL. Connecting in 5 seconds. Op Id:  %d ",iIntervalWaitId );
       
   618         KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StartWaitTimerL");
       
   619         return;
       
   620         }
       
   621 
       
   622 
       
   623     //if we are retrying a connection. n quick retries and then qive up
       
   624     if ( ( iRetryCounter % KAOMaxRetries ) != 0 )
       
   625         {
       
   626         TTimeIntervalSeconds seconds( 0 );
       
   627         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartWaitTimerL() iRetryCounter %d", iRetryCounter);
       
   628 
       
   629         switch ( (iRetryCounter % KAOMaxRetries) )
       
   630             {
       
   631             //values for these retry times configured in AlwaysOnlineEmailAgentBase.h
       
   632             case 1:
       
   633             case 2: // CSI: 47 # see comment above
       
   634                 seconds = EEMailAgentReconnectThree;
       
   635                 break;
       
   636             default:
       
   637                 seconds = EEMailAgentReconnectFive;
       
   638                 break;
       
   639             }//switch
       
   640 
       
   641         time += seconds;
       
   642         }//if
       
   643     else if ( iState == EEmailAgentQueued )
       
   644         {
       
   645         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::StartWaitTimerL() EEmailAgentQueued");
       
   646         TTimeIntervalSeconds seconds( 0 );
       
   647         seconds = EEMailAgentReconnectFour;
       
   648         time += seconds;
       
   649         }
       
   650     else if ( !IsEmn() )
       
   651         {
       
   652         TInt minutes = RetrievalIntervalInMinutes( LoadSettingL<TInt>( 
       
   653             TImumDaSettings::EKeyAutoRetrievalInterval, EFalse ));
       
   654         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartWaitTimerL() Minutes to wait: %d", minutes);
       
   655 
       
   656         TTimeIntervalMinutes intervalMinutes( minutes );
       
   657         time += intervalMinutes;
       
   658         }
       
   659 
       
   660     StartTimerOperationL( time, iIntervalWaitId );
       
   661     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartWaitTimerL. Interval timer started. Op Id:  %d ",iIntervalWaitId );
       
   662 
       
   663     iState = EEmailAgentTimerWaiting;
       
   664     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::StartWaitTimerL. Setting state:  %d ",iState );
       
   665     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StartWaitTimerL");
       
   666     }
       
   667 
       
   668 
       
   669 
       
   670 // ----------------------------------------------------------------------------
       
   671 // FetchNewMailL
       
   672 // ----------------------------------------------------------------------------
       
   673 TMsvOp CAlwaysOnlineImap4Agent::FetchNewMailL()
       
   674     {
       
   675     AOLOG_IN( "CAlwaysOnlineImap4Agent::FetchNewMail" );
       
   676     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::FetchNewMailL");
       
   677     // This is completed because Always Online is supposed to only synchronize the headers.
       
   678     TMsvOp opId = KErrNotFound;
       
   679     iState = EEmailAgentFetching;
       
   680     CreateImap4OperationL( opId, 0, ETrue );
       
   681     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::FetchNewMailL");
       
   682     return opId;
       
   683     }
       
   684 
       
   685 // ----------------------------------------------------------------------------
       
   686 // FindInboxL
       
   687 // ----------------------------------------------------------------------------
       
   688 TMsvId CAlwaysOnlineImap4Agent::FindInboxL()
       
   689     {
       
   690     AOLOG_IN( "CAlwaysOnlineImap4Agent::FindInboxL" );
       
   691     TMsvId inboxId = KErrNotFound;
       
   692     TMsvEntry child;
       
   693     const TInt count = iEntry->Count();
       
   694     _LIT( KInboxName, "Inbox");
       
   695 
       
   696     for ( TInt loop = 0; loop < count && inboxId == KErrNotFound; loop++ )
       
   697         {
       
   698         child = (*iEntry)[loop];
       
   699         if ( child.iType == KUidMsvFolderEntry  &&
       
   700              child.iDetails.CompareF( KInboxName ) == 0 )
       
   701             {
       
   702             inboxId = child.Id();
       
   703             }
       
   704         }
       
   705     return inboxId;
       
   706     }
       
   707 
       
   708 // ----------------------------------------------------------------------------
       
   709 // HandleOpErrorL
       
   710 // ----------------------------------------------------------------------------
       
   711 void CAlwaysOnlineImap4Agent::HandleOpErrorL()
       
   712     {
       
   713     AOLOG_IN( "CAlwaysOnlineImap4Agent::HandleOpErrorL" );
       
   714     AOLOG_WRMV("iError: ", EAoNormalError, iError, EAoLogSt3);
       
   715     AOLOG_WRMV( "start iState: ", EAoMailPluginStates, iState, EAoLogSt3 );
       
   716     KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL() Error Handling started" );
       
   717     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpErrorL() Error ID: %d", iError );
       
   718     switch ( iError )
       
   719         {
       
   720         case KErrNone:
       
   721             //shouldn't come here if KErrNone
       
   722             break;
       
   723         case KErrAbort:
       
   724         case KErrCancel:
       
   725             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL(): KErrCancel");
       
   726             //sometimes we seem to get these errors with connection operations.
       
   727             //so go to initialised state -> reset all
       
   728             iError = KErrNone;
       
   729             iState = EEmailAgentInitialised; //this will be changed where those timers were cancelled.
       
   730             break;
       
   731 
       
   732         case KErrGeneral://seems that this is what is given when phone/csd is in use
       
   733             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL(): KErrGeneral");
       
   734             iState = EEmailAgentQueued;
       
   735             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   736             break;
       
   737         case KErrInUse:
       
   738             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL(): KErrInUse");
       
   739             iState = EEmailAgentQueued;
       
   740             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   741             break;
       
   742             //fatal errors, cannot recover without user intervention
       
   743         case KErrImapServerNoSecurity:
       
   744         case KErrImapTLSNegotiateFailed:
       
   745         case KImskSSLTLSNegotiateFailed:
       
   746         case KErrImapBadLogon:
       
   747         case KErrCorrupt:
       
   748             {
       
   749             iState = EEmailAgentFatalError;
       
   750             iRetryCounter = 0;
       
   751             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpErrorL. Fatal Error: %d.",iError );
       
   752             KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpErrorL. Setting state:  %d ",iState );
       
   753             }
       
   754             break;
       
   755 
       
   756         case KErrAoServerNotFound:
       
   757             HandleDefaultError();
       
   758             break;
       
   759         case KErrConnectionTerminated:
       
   760         case KErrTimedOut:
       
   761             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL. KErrConnectionTimedOut(-33) OR KErrConnectionTerminated");
       
   762             iState = EEmailAgentConnTerminated;
       
   763             break;
       
   764 
       
   765         //just let these go to default retry mechanism
       
   766 
       
   767         case KErrImapServerLoginDisabled:
       
   768         case KErrImapServerParse:
       
   769         case KErrImapServerBusy:
       
   770         case KErrImapServerVersion:
       
   771         case KErrImapSendFail:
       
   772         case KErrImapSelectFail:
       
   773         case KErrImapWrongFolder:
       
   774         case KErrImapCantDeleteFolder:
       
   775         case KErrImapInvalidServerResponse:
       
   776         case KImskErrorDNSNotFound:
       
   777         case KImskErrorControlPanelLocked:
       
   778         case KImskErrorISPOrIAPRecordNotFound:
       
   779         case KImskErrorActiveSettingIsDifferent:
       
   780         case KImskSecuritySettingsFailed:
       
   781         case KErrImapConnectFail:
       
   782         case KErrImapServerFail://how fatal is this?
       
   783         case KErrDisconnected:
       
   784 
       
   785         default:
       
   786             HandleDefaultError();
       
   787             break;
       
   788         };
       
   789     //reset error
       
   790     iError = KErrNone;
       
   791     AOLOG_WRMV( "end iState: ", EAoMailPluginStates, iState, EAoLogSt3 );
       
   792     KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpErrorL() iError Has been reset to KErrNone");
       
   793     }
       
   794 
       
   795 // ----------------------------------------------------------------------------
       
   796 // DoSyncDisconnectL
       
   797 // ----------------------------------------------------------------------------
       
   798 void CAlwaysOnlineImap4Agent::DoSyncDisconnectL()
       
   799     {
       
   800     AOLOG_IN( "CAlwaysOnlineImap4Agent::DoSyncDisconnectL" );
       
   801     if( iEntry->Entry().Connected() )
       
   802         {
       
   803         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoSyncDisconnectL() Doing SyncDisconnect! ");
       
   804 
       
   805 		TBuf8<1> dummyParams;
       
   806 	    dummyParams.Zero();
       
   807 		// Needed to change next operation to synchronized
       
   808         CMsvOperationActiveSchedulerWait* wait =
       
   809         	CMsvOperationActiveSchedulerWait::NewLC();
       
   810 
       
   811         CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   812         CleanupStack::PushL( selection );
       
   813 
       
   814         selection->AppendL( iEntry->Entry().iServiceId );
       
   815         iImap4ClientMtm->SwitchCurrentEntryL( iEntry->Entry().iServiceId );
       
   816         CMsvOperation* op = iImap4ClientMtm->InvokeAsyncFunctionL(
       
   817             KIMAP4MTMDisconnect, *selection, dummyParams, wait->iStatus );
       
   818 
       
   819 		// Start wait object
       
   820        	wait->Start();
       
   821        	
       
   822         delete op;
       
   823         CleanupStack::PopAndDestroy( 2, wait ); // selection // CSI: 12,47 # nothing wrong
       
   824         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoSyncDisconnectL() Should be disconnected now! ");
       
   825         }
       
   826     }
       
   827 
       
   828 // ----------------------------------------------------------------------------
       
   829 //  Called from OpCompleted
       
   830 // ----------------------------------------------------------------------------
       
   831 void CAlwaysOnlineImap4Agent::HandleOpCompleted(
       
   832     TMsvOp opId,
       
   833     TInt aCompletionCode )
       
   834     {
       
   835     AOLOG_IN( "CAlwaysOnlineImap4Agent::HandleOpCompleted" );
       
   836     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpCompleted(). Completion code %d",aCompletionCode );
       
   837     KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::HandleOpCompleted(). opId = %d",opId );
       
   838 
       
   839     TInt trapErr = KErrNone;
       
   840     //save error code so we can handle errors elsewhere
       
   841     if ( opId == iConnectAndStayOnlineOpId )
       
   842         {
       
   843         //if this completes, reconnect. KErrDisconnected causes us to invoke retry mechanism
       
   844         iError = aCompletionCode;
       
   845 
       
   846         if ( iDisconnectOpId == KErrNotFound )
       
   847             {
       
   848             //if we ourselves disconnected, we don't want to reconnect
       
   849             if ( iError == KErrNone )
       
   850                 {
       
   851                 //if no real error, set this so we will reconnect
       
   852                 iError = KErrDisconnected;
       
   853                 }
       
   854             }
       
   855         else
       
   856             {
       
   857             iDisconnectOpId = KErrNotFound;
       
   858             }
       
   859 
       
   860         iConnectAndStayOnlineOpId = KErrNotFound;
       
   861         }
       
   862     else if( ( opId == iConnectSyncOpId || 
       
   863                opId == iConnectOpId || 
       
   864                opId == iSyncOpId ) && 
       
   865              aCompletionCode != KErrNone )
       
   866         {
       
   867         iError = aCompletionCode;
       
   868         SetLastUpdateFailed();
       
   869         }
       
   870     else if ( opId == iConnectSyncOpId )
       
   871         {
       
   872         iError = KErrNone;
       
   873         iConnectSyncOpId = KErrNotFound;
       
   874         iRetryCounter = 0; //reset retry counter
       
   875         }
       
   876     else if ( opId == iConnectOpId )
       
   877         {
       
   878         iError = KErrNone;
       
   879         iConnectOpId = KErrNotFound;
       
   880         iRetryCounter = 0; //reset retry counter
       
   881         }
       
   882     else if ( opId == iSyncOpId )
       
   883         {
       
   884         iError = KErrNone;
       
   885         iSyncOpId = KErrNotFound;
       
   886         iRetryCounter = 0; //reset retry counter
       
   887         }
       
   888     else if ( opId == iIntervalWaitId )
       
   889         {
       
   890         iIntervalWaitId = KErrNotFound;
       
   891         }
       
   892     else if ( opId == iWaitForStopOpId )
       
   893         {
       
   894         if ( aCompletionCode == KErrNone )
       
   895             {
       
   896             iWaitForStopOpId = KErrNotFound;
       
   897             TRAP( trapErr, CheckAndHandleSchedulingL() );
       
   898             if ( trapErr != KErrNone )
       
   899                 {
       
   900                 //can't know why leaved. just reset all and restart
       
   901                 ResetAll();
       
   902                 iState = EEmailAgentInitialised;
       
   903                 }
       
   904             }
       
   905         }
       
   906     else if ( opId == iWaitForStartOpId )
       
   907         {
       
   908         if ( aCompletionCode == KErrNone )
       
   909             {
       
   910             iWaitForStartOpId = KErrNotFound;
       
   911             TRAP( trapErr, CheckAndHandleSchedulingL() );
       
   912             if ( trapErr != KErrNone )
       
   913                 {
       
   914                 //can't know why leaved. just reset all and restart
       
   915                 ResetAll();
       
   916                 iState = EEmailAgentInitialised;
       
   917                 }
       
   918             }
       
   919         }
       
   920     else if ( opId == iStartDelayOpId )
       
   921         {
       
   922         iFlags->SetFlag( EAOBFConnectNow );
       
   923         //leave id untouched. We need this only once, on bootup, or when agent is activated via settings
       
   924         //we use this to check is it used already
       
   925         }
       
   926     else if ( opId == iFolderUpdateTimerOpId )
       
   927         {
       
   928         //timer is restarted when BatchEnd operation completes
       
   929         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpCompleted DoFolderSyncL() called, iFolderUpdateTimerOpId reseted");
       
   930         iFolderUpdateTimerOpId = KErrNotFound;
       
   931         TRAP( trapErr, DoFolderSyncL() );
       
   932         if( trapErr != KErrNone )
       
   933                 {
       
   934                 //folder sync failed for some reason. If it's timer isn't restarted
       
   935                 //do it now
       
   936                 if( iFolderUpdateTimerOpId == KErrNotFound )
       
   937                     {
       
   938                     TRAP( trapErr, StartFolderSyncTimerL() );
       
   939                     //no handling for leave. Just give up with folder sync
       
   940                     }
       
   941                 }
       
   942         }
       
   943     else if ( opId == iFolderSyncOpId )
       
   944         {
       
   945         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpCompleted iFolderSyncOpId completed");
       
   946         //restart folder update timer
       
   947         iFolderSyncOpId = KErrNotFound;
       
   948         if ( iImap4StateFlags.Flag(
       
   949             EAlwaysOnlineImap4StartFetchAfterFolderSync ) )
       
   950             {
       
   951             TRAP( trapErr, FetchNewMailL() );
       
   952             iImap4StateFlags.ClearFlag(
       
   953                 EAlwaysOnlineImap4StartFetchAfterFolderSync );
       
   954             }
       
   955         else
       
   956             {
       
   957             TRAP( trapErr, StartFolderSyncTimerL() );
       
   958             }
       
   959         //no handling for leave. Just give up with folder sync
       
   960         }
       
   961     else if ( opId == iFilteredPopulateOpId )
       
   962         {
       
   963         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::HandleOpCompleted iFilteredPopulateOpId completed");
       
   964         //restart folder update timer
       
   965         iFilteredPopulateOpId = KErrNotFound;
       
   966         TRAP( trapErr, StartFolderSyncTimerL() );
       
   967         //no handling for leave. Just give up with folder sync
       
   968         }
       
   969 
       
   970     TRAP_IGNORE( CallNewMessagesL() );
       
   971     }
       
   972 
       
   973 
       
   974 // ----------------------------------------------------------------------------
       
   975 //  CreateCompletedOpL()
       
   976 // ----------------------------------------------------------------------------
       
   977 void CAlwaysOnlineImap4Agent::CreateCompletedOpL()
       
   978     {
       
   979     AOLOG_IN( "CAlwaysOnlineImap4Agent::CreateCompletedOpL" );
       
   980     CreateImap4OperationL( 0, ETrue );//creates completed operation
       
   981     }
       
   982 
       
   983 // ----------------------------------------------------------------------------
       
   984 //  DoFolderSyncL()
       
   985 // ----------------------------------------------------------------------------
       
   986 void CAlwaysOnlineImap4Agent::DoFolderSyncL()
       
   987     {
       
   988     AOLOG_IN( "CAlwaysOnlineImap4Agent::DoFolderSyncL" );
       
   989     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::DoFolderSyncL");
       
   990     //do folder sync using fullsync function. No point in using several separate foldersync functions because
       
   991     //there is no performance benefit and it is also far more complex to implement. Also data traffic is higher
       
   992     //if separate folders are synced
       
   993 
       
   994     if ( iEntry->Entry().Connected() )
       
   995         {
       
   996         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoFolderSyncL() Connected");
       
   997         KAOEMAIL_LOGGER_WRITE_FORMAT("CAlwaysOnlineImap4Agent::DoFolderSyncL() Folder count %d", iFolderArray->Count() );
       
   998         if ( iFolderArray->Count() > 1 &&
       
   999              iFilteredPopulateOpId == KErrNotFound )
       
  1000             {
       
  1001             //only if connected. We shouldn't get here if not connected, but to be sure...
       
  1002             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoFolderSyncL() Folder sync with KIMAP4MTMFullSync started");
       
  1003             CreateImap4OperationL( iFolderSyncOpId,
       
  1004                 KIMAP4MTMFullSync, EFalse );
       
  1005 
       
  1006             // it *can* happen so that fetching of the email is still in progress while this is started, it could be handled so that
       
  1007             // folder sync timer is re-started in that case instead of starting full sync.
       
  1008             // Added iFilteredPopulateOpId == KErrNotFound because of that, maybe that is enough?
       
  1009 
       
  1010             }
       
  1011         else
       
  1012             {
       
  1013             KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoFolderSyncL() just create compeleted operation");
       
  1014             CreateImap4OperationL( iFolderSyncOpId, 0, ETrue );
       
  1015             }
       
  1016         }
       
  1017     else
       
  1018         {
       
  1019         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::DoFolderSyncL() Connection closed, let's try to connect again!");
       
  1020         iState = EEmailAgentConnectingToStayOnline; // let's try to connect again
       
  1021         }
       
  1022     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::DoFolderSyncL");
       
  1023     }
       
  1024 
       
  1025 
       
  1026 
       
  1027 // ----------------------------------------------------------------------------
       
  1028 // CAlwaysOnlineImap4Agent::StartFolderSyncTimerL()
       
  1029 // ----------------------------------------------------------------------------
       
  1030 void CAlwaysOnlineImap4Agent::StartFolderSyncTimerL()
       
  1031     {
       
  1032     AOLOG_IN( "CAlwaysOnlineImap4Agent::StartFolderSyncTimerL" );
       
  1033     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::StartFolderSyncTimerL");
       
  1034     KAOEMAIL_LOGGER_WRITE_FORMAT2("iFolderUpdateTimerOpId: %d, iFilteredPopulateOpId: %d", iFolderUpdateTimerOpId, iFilteredPopulateOpId);
       
  1035     KAOEMAIL_LOGGER_WRITE_FORMAT("iFolderSyncOpId: %d", iFolderSyncOpId );
       
  1036     if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) &&
       
  1037          iFolderUpdateTimerOpId == KErrNotFound &&
       
  1038          iFilteredPopulateOpId == KErrNotFound &&
       
  1039          iFolderSyncOpId == KErrNotFound )
       
  1040         {
       
  1041         TTimeIntervalSeconds folderWait;
       
  1042         TTime home;
       
  1043         home.HomeTime();
       
  1044 
       
  1045         // If there is a temporary agent created (==EMailAoOff), which means
       
  1046         // user has taken connection manually. It is agreed, that in this case
       
  1047         // we must use value returned by CImImap4Settings::SyncRate function.
       
  1048         // Otherwise user would not know what interval is used for updating
       
  1049         // folders.
       
  1050         if ( LoadSettingL<TInt>(
       
  1051             TImumDaSettings::EKeyAutoRetrieval, EFalse ) !=
       
  1052                 TImumDaSettings::EValueAutoOff )
       
  1053             {
       
  1054             //refresh time must be less than 30 min, because we are in
       
  1055             // iStayOnline -mode take off 2 minutes if limit is set to 30min
       
  1056             // because of IMAP protocoll 29 minute timeout. 28 is safe enough
       
  1057             TInt interval = RetrievalIntervalInMinutes( LoadSettingL<TInt>( 
       
  1058                 TImumDaSettings::EKeyAutoRetrievalInterval, EFalse ));
       
  1059             folderWait = interval * KAlwaysOnlineMinuteToSecondsMultiplier;
       
  1060             if ( interval == KAlwaysOnlineMaxLimit )
       
  1061                 {
       
  1062                 folderWait = ( folderWait.Int() - KAlwaysOnlineTwoMinutes );
       
  1063                 }
       
  1064             }
       
  1065         else
       
  1066             {
       
  1067             folderWait = LoadSettingL<TInt>( TImumInSettings::EKeySyncRate, ETrue );
       
  1068             }
       
  1069 
       
  1070         // Add wait time to current time and start timer.
       
  1071         home += folderWait;
       
  1072 
       
  1073         KAOEMAIL_LOGGER_WRITE_DATETIME("CAlwaysOnlineImap4Agent::StartFolderSyncTimerL(): Folder timer started, will complete at: ", home );
       
  1074         StartTimerOperationL( home, iFolderUpdateTimerOpId );
       
  1075         }//if
       
  1076 
       
  1077     // Start inbox folder observer here...
       
  1078     StartFolderObserverL();
       
  1079     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StartFolderSyncTimerL");
       
  1080 
       
  1081     }
       
  1082 
       
  1083 // ----------------------------------------------------------------------------
       
  1084 // CAlwaysOnlineImap4Agent::AddSubscribedFoldersL()
       
  1085 // ----------------------------------------------------------------------------
       
  1086 void CAlwaysOnlineImap4Agent::UpdateSubscribedFoldersL(
       
  1087     TMsvId aFolderId )
       
  1088     {
       
  1089     AOLOG_IN( "CAlwaysOnlineImap4Agent::UpdateSubscribedFoldersL" );
       
  1090     iFolderArray->Reset();
       
  1091     DoUpdateSubscribedFoldersL( aFolderId );
       
  1092     }
       
  1093 
       
  1094 // ----------------------------------------------------------------------------
       
  1095 // CAlwaysOnlineImap4Agent::DoAddSubscribedFoldersL()
       
  1096 // NOTE: This is recursive!
       
  1097 // ----------------------------------------------------------------------------
       
  1098 void CAlwaysOnlineImap4Agent::DoUpdateSubscribedFoldersL(
       
  1099     TMsvId aFolderId )
       
  1100     {
       
  1101     AOLOG_IN( "CAlwaysOnlineImap4Agent::DoUpdateSubscribedFoldersL" );
       
  1102     CMsvEntry* entry = iSession.GetEntryL( aFolderId );
       
  1103     CleanupStack::PushL( entry );
       
  1104 
       
  1105     const TInt count = entry->Count();
       
  1106     for ( TInt loop = 0; loop < count; loop++ )
       
  1107         {
       
  1108         TMsvEmailEntry mailEntry = (*entry)[loop];
       
  1109         if ( mailEntry.iType.iUid == KUidMsvFolderEntryValue )
       
  1110             {
       
  1111             if ( mailEntry.LocalSubscription() )
       
  1112                 {
       
  1113                 iFolderArray->AppendL( mailEntry.Id() );
       
  1114                 }
       
  1115             DoUpdateSubscribedFoldersL( mailEntry.Id() );
       
  1116             }
       
  1117         }
       
  1118 
       
  1119     CleanupStack::PopAndDestroy( entry );
       
  1120 
       
  1121     }
       
  1122 
       
  1123 // ----------------------------------------------------------------------------
       
  1124 // CAlwaysOnlineImap4Agent::AppendFolderArrayL()
       
  1125 // ----------------------------------------------------------------------------
       
  1126 void CAlwaysOnlineImap4Agent::AppendFolderArrayL(
       
  1127     CMsvEntrySelection& aFolders,
       
  1128     TMsvId aInboxId )
       
  1129     {
       
  1130     AOLOG_IN( "CAlwaysOnlineImap4Agent::AppendFolderArrayL" );
       
  1131     const TInt count = iFolderArray->Count();
       
  1132     TInt i = 0;
       
  1133     for ( i=0; i < count; i++ )
       
  1134         {
       
  1135         if ( (*iFolderArray)[i] != aInboxId )
       
  1136             {
       
  1137             aFolders.AppendL( (*iFolderArray)[i] );
       
  1138             }
       
  1139         }
       
  1140 
       
  1141     }
       
  1142 
       
  1143 
       
  1144 // ----------------------------------------------------------------------------
       
  1145 // CAlwaysOnlineImap4Agent::StartFolderObserverL()
       
  1146 // ----------------------------------------------------------------------------
       
  1147 void CAlwaysOnlineImap4Agent::StartFolderObserverL()
       
  1148     {
       
  1149     AOLOG_IN( "CAlwaysOnlineImap4Agent::StartFolderObserverL" );
       
  1150     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::StartFolderObserverL");
       
  1151     // Start observer only if staying online
       
  1152     if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) )
       
  1153         {
       
  1154         if ( !iFolderObserver )
       
  1155             {
       
  1156             iFolderObserver = CAlwaysOnlineImap4FolderObserver::NewL(
       
  1157                 iSession,
       
  1158                 NULL, // this is set below
       
  1159                 *this );
       
  1160             }
       
  1161         UpdateSubscribedFoldersL( iEntry->Entry().Id() );
       
  1162         iFolderObserver->ResetFoldersL( iFolderArray );
       
  1163         if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4DontReconnect ) )
       
  1164             {
       
  1165             iFolderObserver->SetMailbox( iEntry->Entry().Id() );
       
  1166             }
       
  1167         else
       
  1168             {
       
  1169             iFolderObserver->SetMailbox( 0 );
       
  1170             }
       
  1171         iFolderObserver->Start();
       
  1172         KAOEMAIL_LOGGER_WRITE("Observer started");
       
  1173         }
       
  1174     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StartFolderObserverL");
       
  1175     }
       
  1176 
       
  1177 // ----------------------------------------------------------------------------
       
  1178 // CAlwaysOnlineImap4Agent::StopFolderObserver()
       
  1179 // ----------------------------------------------------------------------------
       
  1180 void CAlwaysOnlineImap4Agent::StopFolderObserver()
       
  1181     {
       
  1182     AOLOG_IN( "CAlwaysOnlineImap4Agent::StopFolderObserver" );
       
  1183     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::StopFolderObserver");
       
  1184     if ( iFolderObserver )
       
  1185         {
       
  1186         iFolderObserver->Stop();
       
  1187         }
       
  1188     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::StopFolderObserver");
       
  1189     }
       
  1190 
       
  1191 // ----------------------------------------------------------------------------
       
  1192 // CAlwaysOnlineImap4Agent::HandleFolderEntryL()
       
  1193 // ----------------------------------------------------------------------------
       
  1194 void CAlwaysOnlineImap4Agent::HandleFolderEntryL(
       
  1195     const CMsvEntrySelection& aFolderArray  )
       
  1196     {
       
  1197     AOLOG_IN( "CAlwaysOnlineImap4Agent::HandleFolderEntryL" );
       
  1198     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::HandleFolderEntryL");
       
  1199 #ifdef _DEBUG
       
  1200     const TInt count = aFolderArray.Count();
       
  1201     for ( TInt i = 0; i < count; i++ )
       
  1202         {
       
  1203         KAOEMAIL_LOGGER_WRITE_FORMAT( "folderId 0x%x", aFolderArray[i] );
       
  1204         }
       
  1205 #endif
       
  1206     TInt index = aFolderArray.Find( iEntry->Entry().Id() );
       
  1207     if ( index > KErrNotFound )
       
  1208         {
       
  1209         // disconnected? Remove myself.
       
  1210         if( !iEntry->Entry().Connected() )
       
  1211             {
       
  1212             // this is a bit dirty way. This agent is removed after disconnect is completed
       
  1213             KAOEMAIL_LOGGER_WRITE( "Mailbox disconnected, kill myself!");
       
  1214             iImap4StateFlags.SetFlag( EAlwaysOnlineImap4RemoveMeImmediately );
       
  1215             DisconnectL( ETrue );
       
  1216             return;
       
  1217             }
       
  1218         }
       
  1219 
       
  1220 
       
  1221     // Start populate operation
       
  1222     // Change this so that fetch new started after folder sync, if it is going, if not,
       
  1223     // then just start fetch new immediately
       
  1224     if ( iFolderSyncOpId > KErrNotFound )
       
  1225         {
       
  1226         StopFolderObserver();
       
  1227         KAOEMAIL_LOGGER_WRITE( "Wait folder sync operation to complete");
       
  1228         iImap4StateFlags.SetFlag( EAlwaysOnlineImap4StartFetchAfterFolderSync );
       
  1229         }
       
  1230     // NOTE: Always Online is supposed to get only the headers, no need to fetch the bodies for mails
       
  1231     // in subscribed folders!!!
       
  1232     else
       
  1233         {
       
  1234         TMsvOp opId;
       
  1235         iState = EEmailAgentFetching;
       
  1236         CreateImap4OperationL( opId, 0, ETrue );
       
  1237         KAOEMAIL_LOGGER_WRITE( "Compliting operation in HandleFolderEntryL");
       
  1238         }
       
  1239     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::HandleFolderEntryL");
       
  1240     }
       
  1241 
       
  1242 // ---------------------------------------------------------------------------
       
  1243 // CAlwaysOnlineImap4Agent::FillMailOptionsL()
       
  1244 // ---------------------------------------------------------------------------
       
  1245 //
       
  1246 TImImap4GetPartialMailInfo CAlwaysOnlineImap4Agent::FillMailOptionsL()
       
  1247     {
       
  1248     AOLOG_IN( "CAlwaysOnlineImap4Agent::FillMailOptionsL" );
       
  1249     TImImap4GetPartialMailInfo getMailInfo;
       
  1250 
       
  1251     TInt syncFlags = LoadSettingL<TInt>(
       
  1252         TImumInSettings::EKeySyncFlags, ETrue );
       
  1253 
       
  1254     if ( syncFlags == TImumInSettings::EFlagDownloadHeader )
       
  1255         {
       
  1256         getMailInfo.iGetMailBodyParts = EGetImap4EmailHeaders;
       
  1257         getMailInfo.iPartialMailOptions = ENoSizeLimits;
       
  1258         }
       
  1259     else
       
  1260         {
       
  1261         // Currently, other than headers can be retrieved
       
  1262         User::Leave( KErrNotSupported );
       
  1263         }
       
  1264 
       
  1265     // Set size definitions
       
  1266     getMailInfo.iBodyTextSizeLimit =
       
  1267         LoadSettingL<TInt32>( TImumInSettings::EKeyDownloadBodySize, ETrue );
       
  1268     getMailInfo.iAttachmentSizeLimit =
       
  1269         LoadSettingL<TInt32>( TImumInSettings::EKeyDownloadAttachmentSize, ETrue );
       
  1270     getMailInfo.iTotalSizeLimit =
       
  1271         getMailInfo.iAttachmentSizeLimit + getMailInfo.iBodyTextSizeLimit;
       
  1272     getMailInfo.iMaxEmailSize = getMailInfo.iTotalSizeLimit;
       
  1273 
       
  1274     if ( getMailInfo.iMaxEmailSize == 0 )
       
  1275         {
       
  1276         getMailInfo.iGetMailBodyParts = EGetImap4EmailHeaders;
       
  1277         }
       
  1278     getMailInfo.iDestinationFolder = KMsvNullIndexEntryId; // No destination for populate.
       
  1279 
       
  1280     return getMailInfo;
       
  1281     }
       
  1282 
       
  1283 // ----------------------------------------------------------------------------
       
  1284 // CAlwaysOnlineImap4Agent::FetchNewMailFromSelectedFoldersL()
       
  1285 // ----------------------------------------------------------------------------
       
  1286 TMsvOp CAlwaysOnlineImap4Agent::FetchNewMailFromSelectedFoldersL(
       
  1287     const CMsvEntrySelection& aFolders )
       
  1288     {
       
  1289     AOLOG_IN( "CAlwaysOnlineImap4Agent::FetchNewMailFromSelectedFoldersL" );
       
  1290     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::FetchNewMailFromSelectedFoldersL");
       
  1291     TMsvOp opId = 0;
       
  1292 
       
  1293     TImImap4GetPartialMailInfo getMailInfo = FillMailOptionsL();
       
  1294 
       
  1295     KAOEMAIL_LOGGER_WRITE_FORMAT("Fetching size limit: %d", getMailInfo.iBodyTextSizeLimit);
       
  1296 
       
  1297     iState = EEmailAgentFetching;
       
  1298     if ( getMailInfo.iGetMailBodyParts == EGetImap4EmailHeaders )
       
  1299         {
       
  1300         // Just complete since filtering is not in use
       
  1301         KAOEMAIL_LOGGER_WRITE_FORMAT("Setting is just headers, create completed operation opId = %d", opId );
       
  1302         CreateImap4OperationL( opId, 0, ETrue );
       
  1303         }
       
  1304     else
       
  1305         {
       
  1306         // read imap filtering settings
       
  1307         KAOEMAIL_LOGGER_WRITE_FORMAT("folder count = %d", aFolders.Count());
       
  1308 
       
  1309         CMsvSingleOpWatcher* watcher = CMsvSingleOpWatcher::NewL(*this);
       
  1310         CleanupStack::PushL( watcher );
       
  1311         CMsvOperation* op = CAlwaysOnlineImap4FolderPopulate::NewL(
       
  1312             iSession,
       
  1313             *iImap4ClientMtm,
       
  1314             iEntry->Entry().Id(),
       
  1315             iInboxId,
       
  1316             getMailInfo,
       
  1317             &aFolders,
       
  1318             watcher->iStatus );
       
  1319 
       
  1320         CleanupStack::PushL( op );
       
  1321         opId = op->Id();
       
  1322         AppendWatcherAndSetOperationL( watcher, op ); // takes immediately ownership
       
  1323 
       
  1324         CleanupStack::Pop( 2, watcher); // op // CSI: 12,47 # nothing wrong
       
  1325         }
       
  1326     iFilteredPopulateOpId = opId;
       
  1327     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::FetchNewMailFromSelectedFoldersL");
       
  1328     return opId;
       
  1329     }
       
  1330 
       
  1331 // ----------------------------------------------------------------------------
       
  1332 // SetUpdateMailWhileConnectedL()
       
  1333 // ----------------------------------------------------------------------------
       
  1334 void CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL(
       
  1335     TBool aAgentAlreadyCreated )
       
  1336     {
       
  1337     AOLOG_IN( "CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL" );
       
  1338     KAOEMAIL_LOGGER_FN1("CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL");
       
  1339 
       
  1340     if ( iImap4StateFlags.Flag( EAlwaysOnlineImap4StayOnline ) )
       
  1341         {
       
  1342         // we are already observing...
       
  1343         KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL ignore");
       
  1344         return;
       
  1345         }
       
  1346 
       
  1347     if ( aAgentAlreadyCreated )
       
  1348         {
       
  1349         iImap4StateFlags.SetFlag( EAlwaysOnlineImap4DontRemoveOnDisconnect );
       
  1350         }
       
  1351 
       
  1352     iImap4StateFlags.SetFlag( EAlwaysOnlineImap4TemporaryConnectionObserver );
       
  1353     iImap4StateFlags.SetFlag( EAlwaysOnlineImap4DontReconnect );
       
  1354     iImap4ClientMtm->SwitchCurrentEntryL( iEntry->Entry().Id() );
       
  1355 
       
  1356     if( iImap4ClientMtm->Entry().Entry().Connected() )
       
  1357         {
       
  1358         iImap4StateFlags.SetFlag( EAlwaysOnlineImap4StayOnline );
       
  1359         iState = EEmailAgentIdle;
       
  1360         StartFolderSyncTimerL();
       
  1361         KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL 1");
       
  1362         return;
       
  1363         }
       
  1364 
       
  1365     // this is a bit dirty way. This agent is removed after disconnect is completed
       
  1366     KAOEMAIL_LOGGER_WRITE( "Mailbox disconnected even we should be connected, kill myself!");
       
  1367     iImap4StateFlags.SetFlag( EAlwaysOnlineImap4RemoveMeImmediately );
       
  1368     DisconnectL( ETrue ); // this creates just completed operation if service is not connected.
       
  1369     KAOEMAIL_LOGGER_FN2("CAlwaysOnlineImap4Agent::SetUpdateMailWhileConnectedL 2");
       
  1370     }
       
  1371 
       
  1372 // ----------------------------------------------------------------------------
       
  1373 // IsTemporary()
       
  1374 // ----------------------------------------------------------------------------
       
  1375 TBool CAlwaysOnlineImap4Agent::IsTemporary() const
       
  1376     {
       
  1377     AOLOG_IN( "CAlwaysOnlineImap4Agent::IsTemporary" );
       
  1378     // If temporary agent is running, then flag EAlwaysOnlineImap4TemporaryConnectionObserver
       
  1379     // is set. At this stage, showing of notes are not allowed, since user may get
       
  1380     // confused with the disk full note!
       
  1381     TBool result =
       
  1382         iImap4StateFlags.Flag( EAlwaysOnlineImap4TemporaryConnectionObserver );
       
  1383 
       
  1384     if ( result )
       
  1385         {
       
  1386         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::IsTemporary: Yes, I am temporary! No notes, plz!" );
       
  1387         return ETrue;
       
  1388         }
       
  1389     else
       
  1390         {
       
  1391         KAOEMAIL_LOGGER_WRITE("CAlwaysOnlineImap4Agent::IsTemporary: No, I am not temporary! Go ahead and show notes, plz!" );
       
  1392         return EFalse;
       
  1393         }
       
  1394     }
       
  1395 
       
  1396 
       
  1397 //EOF