ipsservices/ipssosaoplugin/src/IpsSosAOMboxLogic.cpp
changeset 0 8466d47a6819
child 4 e7aa27f58ae1
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2008 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 *     Contains mailbox specified always online logic
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //<cmail> internal dependency removed
       
    21 /*#ifdef _DEBUG
       
    22 #include "../../internal/IpsSosAOPluginTester/inc/IpsSosAOPluginTester.hrh"
       
    23 #endif // _DEBUG*/
       
    24 //</cmail>
       
    25 
       
    26 #include "emailtrace.h"
       
    27 #include "IpsSosAOMboxLogic.h"
       
    28 #include "ipssetdataapi.h"
       
    29 #include "IpsSosAOSchedulerUtils.h"
       
    30 #include "IpsSosAOPlugin.hrh"
       
    31 #include "IpsSosAOImapPopLogic.h"
       
    32 
       
    33 const TInt KAOSecondsInMinute = 60;
       
    34 const TInt KIpsSosAOMboxLogicMinGra = 1;
       
    35 
       
    36 // ----------------------------------------------------------------------------
       
    37 // ----------------------------------------------------------------------------
       
    38 //
       
    39 CIpsSosAOMBoxLogic::CIpsSosAOMBoxLogic( 
       
    40         CMsvSession& aSession, 
       
    41         TMsvId aMailboxId ) :
       
    42     iSession( aSession ), iMailboxId( aMailboxId ), iState( EStateError ),
       
    43     iErrorCounter( 0 ), iError( KErrNone ), iTimer( NULL ),
       
    44     iFetchMsgArray( KIpsSosAOMboxLogicMinGra ), iDataApi( NULL ), iAgent( NULL ),
       
    45     iExtendedSettings( NULL ), iIsRoaming( EFalse )
       
    46     {
       
    47     FUNC_LOG;
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // ----------------------------------------------------------------------------
       
    52 //
       
    53 CIpsSosAOMBoxLogic::~CIpsSosAOMBoxLogic()
       
    54     {
       
    55     FUNC_LOG;
       
    56     delete iTimer;
       
    57     delete iDataApi;
       
    58     delete iExtendedSettings;
       
    59     delete iAgent;
       
    60     iFetchMsgArray.Close();
       
    61     }
       
    62 
       
    63 // ----------------------------------------------------------------------------
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CIpsSosAOMBoxLogic* CIpsSosAOMBoxLogic::NewL( 
       
    67         CMsvSession& aSession,
       
    68         TMsvId aMailboxId )
       
    69     {
       
    70     FUNC_LOG;
       
    71     CIpsSosAOMBoxLogic* self = new (ELeave) CIpsSosAOMBoxLogic( 
       
    72             aSession,  aMailboxId );
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // ----------------------------------------------------------------------------
       
    80 // ----------------------------------------------------------------------------
       
    81 //
       
    82 void CIpsSosAOMBoxLogic::ConstructL()
       
    83     {
       
    84     FUNC_LOG;
       
    85     iTimer = CIpsSosAOPluginTimer::NewL( CActive::EPriorityStandard, *this );
       
    86     iDataApi = CIpsSetDataApi::NewL( iSession );
       
    87     iAgent = CIpsSosAOBaseAgent::CreateAgentL( iSession, *this, iMailboxId );
       
    88     LoadSettingsL();
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // ----------------------------------------------------------------------------
       
    93 //   
       
    94 void CIpsSosAOMBoxLogic::TimerFiredL()
       
    95     {
       
    96     FUNC_LOG;
       
    97     HandleEventAndSwitchStateL( EEventTimerFired );
       
    98     }
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // ----------------------------------------------------------------------------
       
   102 //   
       
   103 void CIpsSosAOMBoxLogic::OperationCompletedL( TInt aError )
       
   104     {
       
   105     FUNC_LOG;
       
   106     iError = aError;
       
   107     HandleEventAndSwitchStateL( EEventOperationCompleted );
       
   108     iError = KErrNone;
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // ----------------------------------------------------------------------------
       
   113 //  
       
   114 void CIpsSosAOMBoxLogic::SendCommandL( TInt aCommand )
       
   115     {
       
   116     FUNC_LOG;
       
   117     switch ( aCommand )
       
   118         {
       
   119         case ECommandStart:
       
   120             HandleEventAndSwitchStateL( EEventStart );
       
   121             break;
       
   122         case ECommandStop:
       
   123             HandleEventAndSwitchStateL( EEventStopAndRemoveOps );
       
   124             break;
       
   125         case ECommandSuspend:
       
   126             HandleEventAndSwitchStateL( EEventSuspendOperations );
       
   127             break;
       
   128         case ECommandContinue:
       
   129             HandleEventAndSwitchStateL( EEventContinueOperations );
       
   130             break;
       
   131         case ECommandStartSync:
       
   132             HandleEventAndSwitchStateL( EEventStartSync );
       
   133             if ( iState != EStateSyncOngoing )
       
   134                 {
       
   135                 User::Leave( KErrGeneral );
       
   136                 }
       
   137             break;
       
   138         case ECommandStartedRoaming:
       
   139             iIsRoaming = ETrue;
       
   140             if ( !CanConnectIfRoamingL() )
       
   141                 {
       
   142                 HandleEventAndSwitchStateL( EEventStopAndRemoveOps );
       
   143                 }
       
   144             break;
       
   145         case ECommandStoppedRoaming:
       
   146             iIsRoaming = EFalse;
       
   147             HandleEventAndSwitchStateL( EEventStart );
       
   148             break;
       
   149         case ECommandCancelAndDisconnect:
       
   150             iAgent->CancelAllAndDisconnectL();
       
   151             CalculateToNextIntervalL();
       
   152             break;
       
   153         case ECommandCancelDoNotDiconnect:
       
   154             if ( iAgent->GetState() != CIpsSosAOBaseAgent::EStateCompleted ||
       
   155                     iAgent->GetState() != CIpsSosAOBaseAgent::EStateIdle  )
       
   156                 {
       
   157                 iAgent->CancelAllAndDoNotDisconnect();
       
   158                 CalculateToNextIntervalL();
       
   159                 }
       
   160             break;
       
   161         case ECommandClearDoNotDisconnect:
       
   162             iAgent->ClearDoNotDisconnect();
       
   163             break;
       
   164         case ECommandDoNotDisconnect:
       
   165             iAgent->DoNotDisconnect();
       
   166             break;
       
   167         default:
       
   168             break;
       
   169         }
       
   170     }
       
   171 
       
   172 // ----------------------------------------------------------------------------
       
   173 // ----------------------------------------------------------------------------
       
   174 //  
       
   175 void CIpsSosAOMBoxLogic::FetchMessagesL( const RArray<TMsvId> aArray )
       
   176     {
       
   177     FUNC_LOG;
       
   178     LoadSettingsL();
       
   179     for ( TInt i = 0; i < aArray.Count(); i++ )
       
   180         {
       
   181         iFetchMsgArray.AppendL( aArray[i] );
       
   182         }
       
   183     HandleEventAndSwitchStateL( EEventFetchMessages );
       
   184     }
       
   185 
       
   186 // ----------------------------------------------------------------------------
       
   187 // ----------------------------------------------------------------------------
       
   188 //  
       
   189 TMsvId CIpsSosAOMBoxLogic::GetMailboxId()
       
   190     {
       
   191     FUNC_LOG;
       
   192     return iMailboxId;
       
   193     }
       
   194 
       
   195 // ----------------------------------------------------------------------------
       
   196 // ----------------------------------------------------------------------------
       
   197 //  
       
   198 CIpsSosAOBaseAgent& CIpsSosAOMBoxLogic::GetAgent() const
       
   199     {
       
   200     FUNC_LOG;
       
   201     return *iAgent;
       
   202     }
       
   203 
       
   204 // ----------------------------------------------------------------------------
       
   205 // ----------------------------------------------------------------------------
       
   206 // 
       
   207 TBool CIpsSosAOMBoxLogic::IsEmnOn() const
       
   208     {
       
   209     FUNC_LOG;
       
   210     TBool ret = ETrue;
       
   211     TIpsSetDataEmnStates state = EMailEmnOff;
       
   212     if ( iExtendedSettings )
       
   213         {
       
   214         state = iExtendedSettings->EmailNotificationState();
       
   215         }
       
   216     if ( state == EMailEmnOff )
       
   217         {
       
   218         ret = EFalse;
       
   219         }
       
   220     return ret;
       
   221     }
       
   222 
       
   223 // ----------------------------------------------------------------------------
       
   224 // ----------------------------------------------------------------------------
       
   225 // 
       
   226 void CIpsSosAOMBoxLogic::GetEmailAddress(  
       
   227     TBuf<KIpsSosAOTextBufferSize>& aEmailAddress ) const
       
   228     {
       
   229     FUNC_LOG;
       
   230     const TDesC& addr = iExtendedSettings->EmailAddress();
       
   231     __ASSERT_DEBUG( ( 
       
   232             addr.Length() <= KIpsSosAOTextBufferSize ), 
       
   233             User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   234     
       
   235     if ( addr.Length() <= KIpsSosAOTextBufferSize )
       
   236         {
       
   237         aEmailAddress.Copy( addr );
       
   238         }
       
   239     }
       
   240 
       
   241 // ----------------------------------------------------------------------------
       
   242 // ----------------------------------------------------------------------------
       
   243 //  
       
   244 //<cmail>
       
   245 void CIpsSosAOMBoxLogic::SetEmnReceivedFlagL( TBool aNewValue )
       
   246 //</cmail>
       
   247     {
       
   248     LoadSettingsL();
       
   249     iExtendedSettings->SetEmnReceivedButNotSyncedFlag( aNewValue );
       
   250     iDataApi->SaveExtendedSettingsL(
       
   251         *iExtendedSettings );
       
   252     }
       
   253 
       
   254 // ----------------------------------------------------------------------------
       
   255 // ----------------------------------------------------------------------------
       
   256 //
       
   257 //<cmail>
       
   258 TBool CIpsSosAOMBoxLogic::IsMailboxRoamingStoppedL()
       
   259 //</cmail>
       
   260     {
       
   261     TBool ret = EFalse;
       
   262     if ( iIsRoaming && !CanConnectIfRoamingL() )
       
   263         {
       
   264         ret = ETrue;
       
   265         }
       
   266     return ret;
       
   267     }
       
   268 
       
   269 // ----------------------------------------------------------------------------
       
   270 // ----------------------------------------------------------------------------
       
   271 //  
       
   272 void CIpsSosAOMBoxLogic::HandleEventAndSwitchStateL( 
       
   273         TInt aEvent )
       
   274     {
       
   275     FUNC_LOG;
       
   276     TInt event = aEvent;
       
   277     TInt cycles = 0;
       
   278     
       
   279     if ( event == EEventStart && iState == EStateError && 
       
   280             CanConnectIfRoamingL() )
       
   281         {
       
   282         event = EEventNop;
       
   283         iAgent->LoadSettingsL();
       
   284         iTimer->Cancel();
       
   285         iTimer->After( KIpsSosAOStartDelaySeconds );
       
   286         iState = EStateWaitSyncStart;
       
   287         }
       
   288     
       
   289     while ( event != EEventNop && iState != EStateError )
       
   290         {
       
   291 //<cmail> removing flags    
       
   292 /*#ifdef IPSSOSIMAPPOLOGGING_ON
       
   293         WriteDebugData( event );
       
   294 #endif*/
       
   295 //</cmail>
       
   296         switch ( event )
       
   297             {
       
   298             case EEventTimerFired:
       
   299                 event = HandleTimerFiredL();
       
   300               break;
       
   301             case EEventStartSync:
       
   302                 if ( iState == EStateWaitSyncStart )
       
   303                     {         
       
   304                     iError = KErrNone;
       
   305                     iAgent->StartSyncL();
       
   306                     iState = EStateSyncOngoing;
       
   307                     }
       
   308                 else if ( iState == EStateIdleAndWaitCommands )
       
   309                     {
       
   310                     // start sync command in idle state is currently 
       
   311                     // received when emn arrives, checking scheduling 
       
   312                     // if user have selected week days / hours in setting
       
   313                     CIpsSosAOSchedulerUtils* scheduler = 
       
   314                         CIpsSosAOSchedulerUtils::NewLC( *iExtendedSettings );
       
   315                     TTimeIntervalSeconds seconds;
       
   316                     TIpsSosAOConnectionType type = 
       
   317                         scheduler->SecondsToNextMark( seconds );
       
   318                     if ( type == EAOCConnectAfter )
       
   319                         {
       
   320                         iState = EStateWaitSyncStart;
       
   321                         iTimer->After( seconds );
       
   322                         //<cmail>
       
   323                         SetEmnReceivedFlagL( ETrue );
       
   324                         //</cmail>
       
   325                         }
       
   326                     else
       
   327                         {
       
   328                         iError = KErrNone;
       
   329                         iAgent->StartSyncL();
       
   330                         iState = EStateSyncOngoing;
       
   331                         }
       
   332                     CleanupStack::PopAndDestroy( scheduler );
       
   333                     }
       
   334                 // ignore in other states
       
   335                 event = EEventNop;
       
   336                 break;
       
   337             case EEventFetchMessages:
       
   338                 if ( ( iState == EStateWaitSyncStart || 
       
   339                        iState == EStateIdleAndWaitCommands ) &&
       
   340                         iFetchMsgArray.Count() > 0 )
       
   341                     {
       
   342                     iError = KErrNone;
       
   343                     iAgent->StartFetchMessagesL( iFetchMsgArray );
       
   344                     iFetchMsgArray.Reset();
       
   345                     iState = EStateFetchOngoing;
       
   346                     }
       
   347                 else 
       
   348                     {
       
   349                     // ignore event in other states
       
   350                     iFetchMsgArray.Reset();
       
   351                     }
       
   352                 event = EEventNop;
       
   353                 break;
       
   354             case EEventOperationCompleted:
       
   355                 event = HandleOperationCompletionL();
       
   356                 break;
       
   357             case EEventSuspendOperations:
       
   358                 SuspendOperations();
       
   359                 iState = EStateSuspended;
       
   360                 event = EEventNop;
       
   361                 break;
       
   362             case EEventContinueOperations:
       
   363                 if ( iState == EStateSuspended )
       
   364                     {
       
   365                     iTimer->After( KIpsSosAOContinueWaitTime );
       
   366                     }
       
   367                 else
       
   368                     {
       
   369                     event = EEventNop;
       
   370                     }
       
   371                 // ignore if in other states
       
   372                 break;
       
   373             case EEventStopAndRemoveOps:
       
   374                 // notify deletion
       
   375                 iAgent->CancelAllAndDisconnectL();
       
   376                 iTimer->Cancel();
       
   377                 iState = EStateError;
       
   378                 event = EEventNop;
       
   379                 break;
       
   380             case EEventStart:
       
   381             default:
       
   382                 // ignore other events
       
   383                 event = EEventNop;
       
   384                 break;
       
   385             }
       
   386         
       
   387         // TO Prevent forever loop (and freezing) 
       
   388         // if there is programming error in state machine logic
       
   389         __ASSERT_ALWAYS( ( 
       
   390                 cycles < KIpsSosAOMaxStateCycles ), 
       
   391                 User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   392         ++cycles;
       
   393         }
       
   394     }
       
   395 
       
   396 // ----------------------------------------------------------------------------
       
   397 // ----------------------------------------------------------------------------
       
   398 // 
       
   399 TInt CIpsSosAOMBoxLogic::GetCurrentState() const
       
   400     {
       
   401     FUNC_LOG;
       
   402     return iState;
       
   403     }
       
   404 
       
   405 // ----------------------------------------------------------------------------
       
   406 // ----------------------------------------------------------------------------
       
   407 // 
       
   408 TInt CIpsSosAOMBoxLogic::GetCurrentError() const
       
   409     {
       
   410     FUNC_LOG;
       
   411     return iError;
       
   412     }
       
   413 
       
   414 // ----------------------------------------------------------------------------
       
   415 // ----------------------------------------------------------------------------
       
   416 // 
       
   417 TInt CIpsSosAOMBoxLogic::HandleTimerFiredL()
       
   418     {
       
   419     FUNC_LOG;
       
   420     // panic if we are in wrong state;
       
   421     __ASSERT_DEBUG( ( iState == EStateWaitSyncStart || 
       
   422             iState == EStateSuspended ), 
       
   423             User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   424     
       
   425     // handle emn
       
   426     TInt event = EEventNop;
       
   427 
       
   428     CIpsSosAOBaseAgent::TAgentState agentState = iAgent->GetState();
       
   429     if ( iState == EStateSuspended &&
       
   430         ( agentState == CIpsSosAOBaseAgent::EStateConnectAndSyncOnHold || 
       
   431           agentState == CIpsSosAOBaseAgent::EStatePopulateOnHold ) )
       
   432         {
       
   433         iAgent->ContinueHoldOperations();
       
   434         iState = EStateSyncOngoing;
       
   435         }
       
   436     else if ( iState == EStateSuspended && 
       
   437             agentState == CIpsSosAOBaseAgent::EStateFetchOnHold )
       
   438         {
       
   439         iAgent->ContinueHoldOperations();
       
   440         iState = EStateFetchOngoing;
       
   441         }
       
   442     else
       
   443         {
       
   444         event = CheckSchedulingAndSwitchStateL();
       
   445         }
       
   446     // in other states, timer event is ignored
       
   447     
       
   448     return event;
       
   449     }
       
   450 
       
   451 // ----------------------------------------------------------------------------
       
   452 // ----------------------------------------------------------------------------
       
   453 // 
       
   454 TInt CIpsSosAOMBoxLogic::HandleOperationCompletionL()
       
   455     {
       
   456     FUNC_LOG;
       
   457     TInt event = EEventNop;
       
   458     if ( iState == EStateSyncOngoing )
       
   459         {
       
   460         if ( !( iError == KErrNone || iError == KErrCancel ) )
       
   461             {
       
   462             ++iErrorCounter;
       
   463             }
       
   464         else
       
   465             {
       
   466             iError = KErrNone;
       
   467             iErrorCounter = 0;
       
   468             }
       
   469         //<cmail>
       
   470         if ( iError != KErrNone && 
       
   471                 IsErrorFatalL( iError ) )
       
   472         //</cmail>
       
   473             {
       
   474             iAgent->CancelAllAndDisconnectL();
       
   475             // switch ao off
       
   476             iExtendedSettings->SetAlwaysOnlineState( EMailAoOff );
       
   477             // emn not swithced of if its going to be "always on" in
       
   478             // future
       
   479             iExtendedSettings->SetEmnReceivedButNotSyncedFlag( EFalse );
       
   480             iDataApi->SaveExtendedSettingsL(
       
   481                 *iExtendedSettings );
       
   482             iState = EStateError;
       
   483             iErrorCounter = 0;
       
   484             }
       
   485         else if ( iError != KErrNone && 
       
   486                 iErrorCounter < KIpsSosAOMaxReTryTimes )
       
   487             {
       
   488             // not fatal error
       
   489             TIpsSetDataAoStates state = iExtendedSettings->AlwaysOnlineState();
       
   490             if ( state == EMailAoOff )
       
   491                 {
       
   492                 iState = EStateIdleAndWaitCommands;
       
   493                 }
       
   494             else
       
   495                 {
       
   496                 if( !iTimer->IsActive() )
       
   497                     {
       
   498                 iTimer->After( KIpsSosAOReTryInterval );
       
   499                     }
       
   500                 iState = EStateWaitSyncStart;
       
   501                 }
       
   502             }
       
   503         else if ( iError != KErrNone && 
       
   504                 iErrorCounter >= KIpsSosAOMaxReTryTimes )
       
   505             {
       
   506             //<cmail>
       
   507             SetEmnReceivedFlagL( EFalse );
       
   508             //</cmail>
       
   509             // not fatal, but all re trys are gone
       
   510             CalculateToNextIntervalL();
       
   511             }
       
   512         else
       
   513             {
       
   514             // no errors
       
   515             // update successfull sync time to settings
       
   516             SaveSuccessfulSyncTimeL();
       
   517             // and adjust timer to sync interval
       
   518             CalculateToNextIntervalL();
       
   519             }
       
   520         }
       
   521     else if ( iState == EStateFetchOngoing )
       
   522         {
       
   523         iError = KErrNone;
       
   524         if ( iTimer->IsActive() )
       
   525             {
       
   526             iState = EStateWaitSyncStart;
       
   527             }
       
   528         else
       
   529             {
       
   530             event = CheckSchedulingAndSwitchStateL();          
       
   531             }
       
   532         }
       
   533     return event;
       
   534     }
       
   535 
       
   536 // ----------------------------------------------------------------------------
       
   537 // ----------------------------------------------------------------------------
       
   538 // 
       
   539 void CIpsSosAOMBoxLogic::SuspendOperations()
       
   540     {
       
   541     FUNC_LOG;
       
   542     if ( iState == EStateSyncOngoing || iState == EStateFetchOngoing )
       
   543         {
       
   544         iAgent->HoldOperations();
       
   545         }
       
   546     iState = EStateSuspended;
       
   547     // set suspend watchdog, if clien not continue this
       
   548     // ensure ao logic to continue
       
   549     if ( !iTimer->IsActive() )
       
   550         {
       
   551         iTimer->After( KIpsSosAOSuspendWatchdogTime );
       
   552         }
       
   553     }
       
   554 
       
   555 // ----------------------------------------------------------------------------
       
   556 // ----------------------------------------------------------------------------
       
   557 //
       
   558 //<cmail>
       
   559 TBool CIpsSosAOMBoxLogic::IsErrorFatalL( TInt aError )
       
   560 //</cmail>
       
   561     {
       
   562     FUNC_LOG;
       
   563     // error can be fatal only if we haven't got successful connect
       
   564     // in mailbox ever and error is something else than KErrNone or
       
   565     // KErrCancel, and retry interval is reached
       
   566     TBool ret = EFalse;
       
   567     LoadSettingsL();
       
   568     TAOInfo info = iExtendedSettings->LastUpdateInfo();
       
   569     if ( aError != KErrNone && aError != KErrCancel 
       
   570             && !info.iUpdateSuccessfulWithCurSettings
       
   571             && iErrorCounter >= KIpsSosAOMaxReTryTimes )
       
   572         {
       
   573         ret = ETrue;
       
   574         }
       
   575     return ret;
       
   576     }
       
   577 
       
   578 // ----------------------------------------------------------------------------
       
   579 // ----------------------------------------------------------------------------
       
   580 //
       
   581 CIpsSosAOMBoxLogic::TMBoxLogicEvent 
       
   582     CIpsSosAOMBoxLogic::CheckSchedulingAndSwitchStateL()
       
   583     {
       
   584      //<cmail> logs removed </cmail>
       
   585     iTimer->Cancel();
       
   586     TMBoxLogicEvent event = EEventNop;
       
   587     TTimeIntervalSeconds secondsToConnect = CalculateScheduledSyncTimeL();
       
   588     //<cmail> logs removed </cmail>
       
   589     if ( secondsToConnect.Int() == 0 )
       
   590         {
       
   591         //check last successfull sync time and check is interval reached
       
   592         TAOInfo info = iExtendedSettings->LastUpdateInfo();
       
   593         if ( !info.iUpdateSuccessfulWithCurSettings )
       
   594             {
       
   595             event = EEventStartSync;
       
   596             }
       
   597         else 
       
   598             {
       
   599             TTime now;
       
   600             now.HomeTime();
       
   601             TInt interval = 
       
   602                 iExtendedSettings->InboxRefreshTime() * KAOSecondsInMinute;
       
   603             TTimeIntervalSeconds secsFromLastSync;
       
   604             now.SecondsFrom( info.iLastSuccessfulUpdate, secsFromLastSync );
       
   605             if ( (secsFromLastSync.Int() > 0) && ( 
       
   606                     secsFromLastSync.Int() < interval ) )
       
   607                 {
       
   608                 // adjust timer to correct sync time
       
   609                 iTimer->After(interval - secsFromLastSync.Int());        
       
   610                 iState = EStateWaitSyncStart;
       
   611                 }
       
   612             else
       
   613                 {
       
   614                 event = EEventStartSync;
       
   615                 }
       
   616             }
       
   617         }
       
   618     else if ( secondsToConnect.Int() == KErrNotFound )
       
   619         {
       
   620         // means that ao is not on (but emn is)
       
   621         iState = EStateIdleAndWaitCommands;
       
   622         if ( iExtendedSettings->EmnReceivedButNotSyncedFlag() )
       
   623             {
       
   624             // got emn when logic was stopped
       
   625             event = EEventStartSync;
       
   626             }
       
   627         }
       
   628     else
       
   629         {
       
   630         iTimer->After( secondsToConnect );
       
   631         iState = EStateWaitSyncStart;
       
   632         }
       
   633     
       
   634     return event;
       
   635     }
       
   636 
       
   637 // ----------------------------------------------------------------------------
       
   638 // ----------------------------------------------------------------------------
       
   639 //
       
   640 void CIpsSosAOMBoxLogic::CalculateToNextIntervalL()
       
   641     {
       
   642     FUNC_LOG;
       
   643     //<cmail> logs removed </cmail>
       
   644     iTimer->Cancel();
       
   645     TTimeIntervalSeconds interval;
       
   646     // first check and calculate scheduling function also loads settings
       
   647     interval = CalculateScheduledSyncTimeL();
       
   648     //<cmail> logs removed </cmail>
       
   649     if ( interval.Int() == 0 )
       
   650         {
       
   651         // add next interval, if interval is grater than 0 it means
       
   652         // that off-line time is going, user have selected days / times from
       
   653         // settings
       
   654         iTimer->After( 
       
   655                 iExtendedSettings->InboxRefreshTime() * KAOSecondsInMinute );
       
   656         iState = EStateWaitSyncStart;
       
   657         }
       
   658     else if ( interval.Int() > 0 )
       
   659         {
       
   660         iTimer->After( interval );
       
   661         iState = EStateWaitSyncStart;
       
   662         }
       
   663     else 
       
   664         {
       
   665         iState = EStateIdleAndWaitCommands;
       
   666         }
       
   667     
       
   668     }
       
   669 
       
   670 // ----------------------------------------------------------------------------
       
   671 // ----------------------------------------------------------------------------
       
   672 //
       
   673 TTimeIntervalSeconds CIpsSosAOMBoxLogic::CalculateScheduledSyncTimeL()
       
   674     {
       
   675     FUNC_LOG;
       
   676     //<cmail> logs removed </cmail> 
       
   677     LoadSettingsL();
       
   678     TTimeIntervalSeconds seconds;
       
   679     
       
   680     // first check is ao or emn on at all
       
   681     if ( (iExtendedSettings->AlwaysOnlineState() == EMailAoOff && 
       
   682             iExtendedSettings->EmailNotificationState() == EMailEmnOff  )||
       
   683         iExtendedSettings->SelectedWeekDays() == 0 ) // no week days chosen
       
   684         {
       
   685         // no timed sync on
       
   686         return KErrNotFound;
       
   687         }
       
   688     
       
   689     if ( iExtendedSettings->EmailNotificationState() != EMailEmnOff && 
       
   690          !iExtendedSettings->EmnReceivedButNotSyncedFlag()   )
       
   691         {
       
   692         return KErrNotFound;
       
   693         }
       
   694     
       
   695     
       
   696     CIpsSosAOSchedulerUtils* scheduler = CIpsSosAOSchedulerUtils::NewLC(
       
   697         *iExtendedSettings );
       
   698 
       
   699     TIpsSosAOConnectionType type = scheduler->SecondsToNextMark( seconds );
       
   700     if ( type == EAOCDisconnectAfter ) 
       
   701         {
       
   702         // if type is EAOCDisconnectAfter it means that scheduled days / times
       
   703         // end after interval function returned in seconds reference
       
   704         // when connection is not kept open we not need timer for 
       
   705         // disconnection. Just return 0 as a mark that sync can be started
       
   706         seconds = 0;
       
   707         }
       
   708     else
       
   709         {
       
   710         // this means that user have choosed time/day schedule at 
       
   711         // timed sync and now we have to wait sync time
       
   712         }
       
   713     CleanupStack::PopAndDestroy( scheduler );
       
   714     return seconds;
       
   715     }
       
   716 
       
   717 // ----------------------------------------------------------------------------
       
   718 // ----------------------------------------------------------------------------
       
   719 //
       
   720 void CIpsSosAOMBoxLogic::LoadSettingsL()
       
   721     {
       
   722     FUNC_LOG;
       
   723     //<cmail> logs removed </cmail> 
       
   724     delete iExtendedSettings;
       
   725     iExtendedSettings = NULL;
       
   726     iExtendedSettings = CIpsSetDataExtension::NewL();
       
   727     iDataApi->LoadExtendedSettingsL( iMailboxId, *iExtendedSettings );
       
   728     //<cmail> logs removed </cmail>
       
   729     }
       
   730 
       
   731 // ----------------------------------------------------------------------------
       
   732 // ----------------------------------------------------------------------------
       
   733 //
       
   734 void CIpsSosAOMBoxLogic::SaveSuccessfulSyncTimeL()
       
   735     {
       
   736     FUNC_LOG;
       
   737     TTime now;
       
   738     now.HomeTime();
       
   739     LoadSettingsL();
       
   740     TAOInfo info;
       
   741     info.iLastSuccessfulUpdate = now;
       
   742     info.iUpdateSuccessfulWithCurSettings = ETrue;
       
   743     iExtendedSettings->SetLastUpdateInfo( info );
       
   744     // clear flag
       
   745     iExtendedSettings->SetEmnReceivedButNotSyncedFlag( EFalse );
       
   746     iDataApi->SaveExtendedSettingsL(
       
   747             *iExtendedSettings );
       
   748     }
       
   749 
       
   750 // ----------------------------------------------------------------------------
       
   751 // ----------------------------------------------------------------------------
       
   752 TBool CIpsSosAOMBoxLogic::CanConnectIfRoamingL()
       
   753     {
       
   754     FUNC_LOG;
       
   755     TBool ret = ETrue;
       
   756     LoadSettingsL();
       
   757     if ( ( iExtendedSettings->EmailNotificationState() == EMailEmnHomeOnly 
       
   758           || iExtendedSettings->AlwaysOnlineState() == EMailAoHomeOnly
       
   759           || iExtendedSettings->RoamHomeOnlyFlag() ) && iIsRoaming )
       
   760         {
       
   761         ret = EFalse;
       
   762         }
       
   763     return ret;          
       
   764     }
       
   765 
       
   766 // ----------------------------------------------------------------------------
       
   767 // ----------------------------------------------------------------------------
       
   768 TBool CIpsSosAOMBoxLogic::FirstEMNReceived()
       
   769     {
       
   770     return iExtendedSettings->FirstEmnReceived();
       
   771     }
       
   772 
       
   773 // ----------------------------------------------------------------------------
       
   774 // ----------------------------------------------------------------------------
       
   775 void CIpsSosAOMBoxLogic::SetFirstEMNReceived()
       
   776     {
       
   777     TRAP_IGNORE( LoadSettingsL() );
       
   778     iExtendedSettings->SetFirstEmnReceived( ETrue );
       
   779     
       
   780     //if alwaysonline was allowed to roam, so will EMN be.
       
   781     iExtendedSettings->SetEmailNotificationState(
       
   782             iExtendedSettings->AlwaysOnlineState() == 
       
   783             EMailAoAlways ? EMailEmnAutomatic : EMailEmnHomeOnly );
       
   784     
       
   785     
       
   786     // set always online state off when emn is on
       
   787     iExtendedSettings->SetAlwaysOnlineState( EMailAoOff );    
       
   788     iExtendedSettings->SetInboxRefreshTime( KErrNotFound );
       
   789     
       
   790     TRAP_IGNORE( iDataApi->SaveExtendedSettingsL(
       
   791        *iExtendedSettings ) );    
       
   792     
       
   793     }
       
   794 //<cmail> removing flags
       
   795 /*
       
   796 #ifdef IPSSOSIMAPPOLOGGING_ON
       
   797 // ----------------------------------------------------------------------------
       
   798 // ----------------------------------------------------------------------------
       
   799 //
       
   800 void CIpsSosAOMBoxLogic::WriteDebugData( TInt aEvent )
       
   801     {
       
   802     switch ( aEvent )
       
   803         {
       
   804         case EEventTimerFired:
       
   805             break;
       
   806         case EEventStartSync:
       
   807             break;
       
   808         case EEventFetchMessages:
       
   809             break;
       
   810         case EEventOperationCompleted:
       
   811             break;
       
   812         case EEventSuspendOperations:
       
   813             break;
       
   814         case EEventContinueOperations:
       
   815             break;
       
   816         case EEventStart:
       
   817             break;
       
   818         case EEventStopAndRemoveOps:
       
   819             break;
       
   820         case EEventNop:
       
   821             break;
       
   822         default:
       
   823             break;
       
   824         }
       
   825 
       
   826     switch ( iState )
       
   827         {
       
   828         case EStateWaitSyncStart:
       
   829             break;
       
   830         case EStateSyncOngoing:
       
   831             break;
       
   832         case EStateFetchOngoing:
       
   833             break;
       
   834         case EStateSuspended:
       
   835             break;
       
   836         case EStateIdleAndWaitCommands:
       
   837             break;
       
   838         case EStateError:
       
   839             break;
       
   840         default:
       
   841             break;
       
   842         };
       
   843     }
       
   844 #endif // IPSSMTPLOGGING_ON*/
       
   845 //</cmail>    
       
   846 
       
   847 // End of file
       
   848