ipsservices/ipssosaoplugin/src/IpsSosAOMboxLogic.cpp
changeset 59 16ed8d08d0b1
parent 30 759dc5235cdb
child 65 478bc57ad291
equal deleted inserted replaced
54:997a02608b3a 59:16ed8d08d0b1
    14 * Description: 
    14 * Description: 
    15 *     Contains mailbox specified always online logic
    15 *     Contains mailbox specified always online logic
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 //<Qmail>
    19 #include "ipssosaopluginheaders.h"
    20 #include <cmmanager.h>
       
    21 //</Qmail>
       
    22 
       
    23 #include "emailtrace.h"
       
    24 #include "IpsSosAOMboxLogic.h"
       
    25 //<QMail>
       
    26 #include "IpsSosAOSettingsHandler.h"
       
    27 //</QMail>
       
    28 #include "IpsSosAOSchedulerUtils.h"
       
    29 #include "IpsSosAOPlugin.hrh"
       
    30 #include "IpsSosAOImapPopLogic.h"
       
    31 
    20 
    32 //<QMail>
    21 //<QMail>
    33 const TInt KAOSecondsInMinute = 60;
    22 const TInt KAOSecondsInMinute = 60;
       
    23 const TInt KAODefaultInboxSyncTimeSecs = 3600;
    34 //</QMail>
    24 //</QMail>
    35 const TInt KIpsSosAOMboxLogicMinGra = 1;
    25 const TInt KIpsSosAOMboxLogicMinGra = 1;
    36 
    26 
    37 // ----------------------------------------------------------------------------
    27 // ----------------------------------------------------------------------------
    38 // ----------------------------------------------------------------------------
    28 // ----------------------------------------------------------------------------
   317     
   307     
   318     if ( event == EEventStart && iState == EStateError && 
   308     if ( event == EEventStart && iState == EStateError && 
   319           ( (iIsRoaming && CanConnectIfRoamingL())||
   309           ( (iIsRoaming && CanConnectIfRoamingL())||
   320              !iIsRoaming ) )
   310              !iIsRoaming ) )
   321         {
   311         {
       
   312         NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventStart");
   322         event = EEventNop;
   313         event = EEventNop;
   323         iAgent->LoadSettingsL();
   314         iAgent->LoadSettingsL();
   324         iTimer->Cancel();
   315         iTimer->Cancel();
       
   316         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", KIpsSosAOStartDelaySeconds);
   325         iTimer->After( KIpsSosAOStartDelaySeconds );
   317         iTimer->After( KIpsSosAOStartDelaySeconds );
   326         iState = EStateWaitSyncStart;
   318         iState = EStateWaitSyncStart;
       
   319         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   327         }
   320         }
   328     
   321     
   329     while ( event != EEventNop && iState != EStateError )
   322     while ( event != EEventNop && iState != EStateError )
   330         {
   323         {
   331         switch ( event )
   324         switch ( event )
   332             {
   325             {
   333             case EEventTimerFired:
   326             case EEventTimerFired:
       
   327                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventTimerFired");
   334                 event = HandleTimerFiredL();
   328                 event = HandleTimerFiredL();
   335               break;
   329               break;
   336             case EEventStartSync:
   330             case EEventStartSync:
       
   331                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventStartSync");
   337                 if ( iState == EStateWaitSyncStart )
   332                 if ( iState == EStateWaitSyncStart )
   338                     {         
   333                     {         
   339                     iError = KErrNone;
   334                     iError = KErrNone;
   340                     iAgent->StartSyncL();
   335                     iAgent->StartSyncL();
   341                     iState = EStateSyncOngoing;
   336                     iState = EStateSyncOngoing;
       
   337                     NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateSyncOngoing");
   342                     }
   338                     }
   343                 else if ( iState == EStateIdleAndWaitCommands )
   339                 else if ( iState == EStateIdleAndWaitCommands )
   344                     {
   340                     {
   345                     // start sync command in idle state is currently 
   341                     // start sync command in idle state is currently 
   346                     // received when emn arrives, checking scheduling 
   342                     // received when emn arrives, checking scheduling 
   358                             scheduler->SecondsToNextMark( seconds );
   354                             scheduler->SecondsToNextMark( seconds );
   359                     
   355                     
   360 					//</QMail>
   356 					//</QMail>
   361                     if ( type == EAOCConnectAfter )
   357                     if ( type == EAOCConnectAfter )
   362                         {
   358                         {
       
   359                         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   363                         iState = EStateWaitSyncStart;
   360                         iState = EStateWaitSyncStart;
       
   361                         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", seconds);
   364                         iTimer->After( seconds );
   362                         iTimer->After( seconds );
   365                         
   363                         
   366                         SetEmnReceivedFlagL( ETrue );
   364                         SetEmnReceivedFlagL( ETrue );
   367                         }
   365                         }
   368                     else
   366                     else
   369                         {
   367                         {
   370                         iError = KErrNone;
   368                         iError = KErrNone;
   371                         iAgent->StartSyncL();
   369                         iAgent->StartSyncL();
   372                         iState = EStateSyncOngoing;
   370                         iState = EStateSyncOngoing;
       
   371                         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateSyncOngoing");
   373                         }
   372                         }
   374 					//<QMail>
   373 					//<QMail>
   375                     CleanupStack::PopAndDestroy( 2, settings );
   374                     CleanupStack::PopAndDestroy( 2, settings );
   376 					//</QMail>
   375 					//</QMail>
   377                     }
   376                     }
   378                 // ignore in other states
   377                 // ignore in other states
   379                 event = EEventNop;
   378                 event = EEventNop;
   380                 break;
   379                 break;
   381             case EEventFetchMessages:
   380             case EEventFetchMessages:
       
   381                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventFetchMessages");
   382                 if ( ( iState == EStateWaitSyncStart || 
   382                 if ( ( iState == EStateWaitSyncStart || 
   383                        iState == EStateIdleAndWaitCommands ) &&
   383                        iState == EStateIdleAndWaitCommands ) &&
   384                         iFetchMsgArray.Count() > 0 )
   384                         iFetchMsgArray.Count() > 0 )
   385                     {
   385                     {
   386                     iError = KErrNone;
   386                     iError = KErrNone;
   387                     iAgent->StartFetchMessagesL( iFetchMsgArray );
   387                     iAgent->StartFetchMessagesL( iFetchMsgArray );
   388                     iFetchMsgArray.Reset();
   388                     iFetchMsgArray.Reset();
       
   389                     NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateFetchOngoing");
   389                     iState = EStateFetchOngoing;
   390                     iState = EStateFetchOngoing;
   390                     }
   391                     }
   391                 else 
   392                 else 
   392                     {
   393                     {
   393                     // ignore event in other states
   394                     // ignore event in other states
   394                     iFetchMsgArray.Reset();
   395                     iFetchMsgArray.Reset();
   395                     }
   396                     }
   396                 event = EEventNop;
   397                 event = EEventNop;
   397                 break;
   398                 break;
   398             case EEventOperationCompleted:
   399             case EEventOperationCompleted:
       
   400                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventOperationCompleted");
   399                 event = HandleOperationCompletionL();
   401                 event = HandleOperationCompletionL();
   400                 break;
   402                 break;
   401             case EEventSuspendOperations:
   403             case EEventSuspendOperations:
       
   404                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventSuspendOperations");
   402                 SuspendOperations();
   405                 SuspendOperations();
   403                 iState = EStateSuspended;
   406                 iState = EStateSuspended;
   404                 event = EEventNop;
   407                 event = EEventNop;
   405                 break;
   408                 break;
   406             case EEventContinueOperations:
   409             case EEventContinueOperations:
       
   410                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventContinueOperations");
   407                 if ( iState == EStateSuspended )
   411                 if ( iState == EStateSuspended )
   408                     {
   412                     {
       
   413                     INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", KIpsSosAOContinueWaitTime);
   409                     iTimer->After( KIpsSosAOContinueWaitTime );
   414                     iTimer->After( KIpsSosAOContinueWaitTime );
   410                     }
   415                     }
   411                 else
   416                 else
   412                     {
   417                     {
   413                     event = EEventNop;
   418                     event = EEventNop;
   414                     }
   419                     }
   415                 // ignore if in other states
   420                 // ignore if in other states
   416                 break;
   421                 break;
   417             case EEventStopAndRemoveOps:
   422             case EEventStopAndRemoveOps:
       
   423                 NM_COMMENT("CIpsSosAOMBoxLogic: event: EEventStopAndRemoveOps");
   418                 // notify deletion
   424                 // notify deletion
   419                 iAgent->CancelAllAndDisconnectL();
   425                 iAgent->CancelAllAndDisconnectL();
   420                 iTimer->Cancel();
   426                 iTimer->Cancel();
       
   427                 NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateError");
   421                 iState = EStateError;
   428                 iState = EStateError;
   422                 event = EEventNop;
   429                 event = EEventNop;
   423                 break;
   430                 break;
   424             case EEventStart:
   431             case EEventStart:
   425             default:
   432             default:
   473     if ( iState == EStateSuspended &&
   480     if ( iState == EStateSuspended &&
   474         ( agentState == CIpsSosAOBaseAgent::EStateConnectAndSyncOnHold || 
   481         ( agentState == CIpsSosAOBaseAgent::EStateConnectAndSyncOnHold || 
   475           agentState == CIpsSosAOBaseAgent::EStatePopulateOnHold ) )
   482           agentState == CIpsSosAOBaseAgent::EStatePopulateOnHold ) )
   476         {
   483         {
   477         iAgent->ContinueHoldOperations();
   484         iAgent->ContinueHoldOperations();
       
   485         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateSyncOngoing");
   478         iState = EStateSyncOngoing;
   486         iState = EStateSyncOngoing;
   479         }
   487         }
   480     else if ( iState == EStateSuspended && 
   488     else if ( iState == EStateSuspended && 
   481             agentState == CIpsSosAOBaseAgent::EStateFetchOnHold )
   489             agentState == CIpsSosAOBaseAgent::EStateFetchOnHold )
   482         {
   490         {
   483         iAgent->ContinueHoldOperations();
   491         iAgent->ContinueHoldOperations();
       
   492         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateFetchOngoing");
   484         iState = EStateFetchOngoing;
   493         iState = EStateFetchOngoing;
   485         }
   494         }
   486     else
   495     else
   487         {
   496         {
   488         event = CheckSchedulingAndSwitchStateL();
   497         event = CheckSchedulingAndSwitchStateL();
   504              CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   513              CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   505     CleanupStack::PushL(settings);
   514     CleanupStack::PushL(settings);
   506 	//</Qmail>     
   515 	//</Qmail>     
   507     if ( iState == EStateSyncOngoing )
   516     if ( iState == EStateSyncOngoing )
   508         {
   517         {
       
   518         INFO_1("CIpsSosAOMBoxLogic: operation completed error: %d", iError);
       
   519         bool doSaveSyncTime = EFalse;
   509         if ( !( iError == KErrNone || iError == KErrCancel ) )
   520         if ( !( iError == KErrNone || iError == KErrCancel ) )
   510             {
   521             {
   511             ++iErrorCounter;
   522             ++iErrorCounter;
   512             }
   523             }
   513         else
   524         else
   514             {
   525             {
       
   526             if (iError == KErrNone)
       
   527                 {
       
   528                 doSaveSyncTime = ETrue;
       
   529                 }
   515             iError = KErrNone;
   530             iError = KErrNone;
   516             iErrorCounter = 0;
   531             iErrorCounter = 0;
   517             }
   532             }
   518         
   533         
   519         if ( iError != KErrNone && 
   534         if ( iError != KErrNone && 
   527             // emn not swithced of if its going to be "always on" in
   542             // emn not swithced of if its going to be "always on" in
   528             // future
   543             // future
   529             
   544             
   530             settings->SetEmnReceivedButNotSyncedFlag( EFalse );
   545             settings->SetEmnReceivedButNotSyncedFlag( EFalse );
   531             //</QMail>
   546             //</QMail>
       
   547             NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateError");
   532             iState = EStateError;
   548             iState = EStateError;
   533             iErrorCounter = 0;
   549             iErrorCounter = 0;
   534             }
   550             }
   535         else if ( iError != KErrNone && 
   551         else if ( iError != KErrNone && 
   536                 iErrorCounter < KIpsSosAOMaxReTryTimes )
   552                 iErrorCounter < KIpsSosAOMaxReTryTimes )
   540             IpsServices::TIpsSetDataAoStates state = 
   556             IpsServices::TIpsSetDataAoStates state = 
   541                     settings->AlwaysOnlineState();
   557                     settings->AlwaysOnlineState();
   542             
   558             
   543             if ( state == IpsServices::EMailAoOff )
   559             if ( state == IpsServices::EMailAoOff )
   544                 {
   560                 {
       
   561                 NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateIdleAndWaitCommands");
   545                 iState = EStateIdleAndWaitCommands;
   562                 iState = EStateIdleAndWaitCommands;
   546                 }
   563                 }
   547             else
   564             else
   548                 {
   565                 {
   549                 if( !iTimer->IsActive() )
   566                 if( !iTimer->IsActive() )
   550                     {
   567                     {
       
   568                     INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", KIpsSosAOReTryInterval);
   551                     iTimer->After( KIpsSosAOReTryInterval );
   569                     iTimer->After( KIpsSosAOReTryInterval );
   552                     }
   570                     }
       
   571                 NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   553                 iState = EStateWaitSyncStart;
   572                 iState = EStateWaitSyncStart;
   554                 }
   573                 }
   555 
   574 
   556 			//</QMail>
   575 			//</QMail>
   557             }
   576             }
   565             }
   584             }
   566         else
   585         else
   567             {
   586             {
   568             // no errors
   587             // no errors
   569             // update successfull sync time to settings
   588             // update successfull sync time to settings
   570             SaveSuccessfulSyncTimeL();
   589             if (doSaveSyncTime)
       
   590                 {
       
   591                 SaveSuccessfulSyncTimeL();
       
   592                 }
   571             // and adjust timer to sync interval
   593             // and adjust timer to sync interval
   572             CalculateToNextIntervalL();
   594             CalculateToNextIntervalL();
   573             }
   595             }
   574         }
   596         }
   575     else if ( iState == EStateFetchOngoing )
   597     else if ( iState == EStateFetchOngoing )
   576         {
   598         {
   577         iError = KErrNone;
   599         iError = KErrNone;
   578         if ( iTimer->IsActive() )
   600         if ( iTimer->IsActive() )
   579             {
   601             {
       
   602             NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   580             iState = EStateWaitSyncStart;
   603             iState = EStateWaitSyncStart;
   581             }
   604             }
   582         else
   605         else
   583             {
   606             {
   584             event = CheckSchedulingAndSwitchStateL();          
   607             event = CheckSchedulingAndSwitchStateL();          
   598     FUNC_LOG;
   621     FUNC_LOG;
   599     if ( iState == EStateSyncOngoing || iState == EStateFetchOngoing )
   622     if ( iState == EStateSyncOngoing || iState == EStateFetchOngoing )
   600         {
   623         {
   601         iAgent->HoldOperations();
   624         iAgent->HoldOperations();
   602         }
   625         }
       
   626     NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateSuspended");
   603     iState = EStateSuspended;
   627     iState = EStateSuspended;
   604     // set suspend watchdog, if clien not continue this
   628     // set suspend watchdog, if clien not continue this
   605     // ensure ao logic to continue
   629     // ensure ao logic to continue
   606     if ( !iTimer->IsActive() )
   630     if ( !iTimer->IsActive() )
   607         {
   631         {
       
   632         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", KIpsSosAOSuspendWatchdogTime);
   608         iTimer->After( KIpsSosAOSuspendWatchdogTime );
   633         iTimer->After( KIpsSosAOSuspendWatchdogTime );
   609         }
   634         }
   610     }
   635     }
   611 
   636 
   612 // ----------------------------------------------------------------------------
   637 // ----------------------------------------------------------------------------
   647 CIpsSosAOMBoxLogic::TMBoxLogicEvent 
   672 CIpsSosAOMBoxLogic::TMBoxLogicEvent 
   648     CIpsSosAOMBoxLogic::CheckSchedulingAndSwitchStateL()
   673     CIpsSosAOMBoxLogic::CheckSchedulingAndSwitchStateL()
   649     {
   674     {
   650 	//<Qmail>
   675 	//<Qmail>
   651     FUNC_LOG;
   676     FUNC_LOG;
   652      iTimer->Cancel();
   677     iTimer->Cancel();
   653     TMBoxLogicEvent event = EEventNop;
   678     TMBoxLogicEvent event = EEventNop;
   654     TTimeIntervalSeconds secondsToConnect = CalculateScheduledSyncTimeL();
   679     TTimeIntervalSeconds secondsToConnect = CalculateScheduledSyncTimeL();
   655     
   680     
   656     CIpsSosAOSettingsHandler* settings = 
   681     CIpsSosAOSettingsHandler* settings = 
   657              CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   682              CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   677             now.SecondsFrom( info.iLastSuccessfulUpdate, secsFromLastSync );
   702             now.SecondsFrom( info.iLastSuccessfulUpdate, secsFromLastSync );
   678             if ( (secsFromLastSync.Int() > 0) && ( 
   703             if ( (secsFromLastSync.Int() > 0) && ( 
   679                     secsFromLastSync.Int() < interval ) )
   704                     secsFromLastSync.Int() < interval ) )
   680                 {
   705                 {
   681                 // adjust timer to correct sync time
   706                 // adjust timer to correct sync time
   682                 iTimer->After(interval - secsFromLastSync.Int());        
   707                 TInt syncAfter = interval - secsFromLastSync.Int();
       
   708                 INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", syncAfter);
       
   709                 iTimer->After(syncAfter); 
       
   710                 NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   683                 iState = EStateWaitSyncStart;
   711                 iState = EStateWaitSyncStart;
   684                 }
   712                 }
   685             else
   713             else
   686                 {
   714                 {
   687                 event = EEventStartSync;
   715                 event = EEventStartSync;
   690         
   718         
   691         }
   719         }
   692     else if ( secondsToConnect.Int() == KErrNotFound )
   720     else if ( secondsToConnect.Int() == KErrNotFound )
   693         {
   721         {
   694         // means that ao is not on (but emn is)
   722         // means that ao is not on (but emn is)
       
   723         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateIdleAndWaitCommands");
   695         iState = EStateIdleAndWaitCommands;
   724         iState = EStateIdleAndWaitCommands;
   696       
   725       
   697         if ( settings->EmnReceivedButNotSyncedFlag() )
   726         if ( settings->EmnReceivedButNotSyncedFlag() )
   698             {
   727             {
   699             // got emn when logic was stopped
   728             // got emn when logic was stopped
   700             event = EEventStartSync;
   729             event = EEventStartSync;
   701             }
   730             }
   702         }
   731         }
   703     else
   732     else
   704         {
   733         {
       
   734         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", secondsToConnect);
   705         iTimer->After( secondsToConnect );
   735         iTimer->After( secondsToConnect );
       
   736         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   706         iState = EStateWaitSyncStart;
   737         iState = EStateWaitSyncStart;
   707         
   738         
   708 
   739 
   709         }
   740         }
   710     CleanupStack::PopAndDestroy(settings);
   741     CleanupStack::PopAndDestroy(settings);
   732 		
   763 		
   733 		//<QMail>
   764 		//<QMail>
   734         CIpsSosAOSettingsHandler* settings = 
   765         CIpsSosAOSettingsHandler* settings = 
   735                  CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   766                  CIpsSosAOSettingsHandler::NewL(iSession, iMailboxId);
   736         CleanupStack::PushL(settings);
   767         CleanupStack::PushL(settings);
   737         iTimer->After( 
   768         TInt inboxRefreshTime = settings->InboxRefreshTime();
   738                 settings->InboxRefreshTime() * KAOSecondsInMinute );
   769         TInt secs = inboxRefreshTime * KAOSecondsInMinute;
       
   770         INFO_1("CIpsSosAOMBoxLogic: inboxRefreshTime: %d", inboxRefreshTime);
       
   771         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", secs);
       
   772         if (secs > 0) 
       
   773             {
       
   774             iTimer->After(TTimeIntervalSeconds(secs));
       
   775             }
       
   776         else 
       
   777             {
       
   778             // panic at this point when we get zero time from settings
       
   779             __ASSERT_DEBUG( EFalse, User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   780             iTimer->After(TTimeIntervalSeconds(KAODefaultInboxSyncTimeSecs));
       
   781             }
       
   782             
   739         CleanupStack::PopAndDestroy(settings);
   783         CleanupStack::PopAndDestroy(settings);
   740 	   //</QMail>
   784 	   //</QMail>
       
   785         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   741         iState = EStateWaitSyncStart;
   786         iState = EStateWaitSyncStart;
   742         }
   787         }
   743     else if ( interval.Int() > 0 )
   788     else if ( interval.Int() > 0 )
   744         {
   789         {
       
   790         INFO_1("CIpsSosAOMBoxLogic: timer scheduled: %d", interval);
   745         iTimer->After( interval );
   791         iTimer->After( interval );
       
   792         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateWaitSyncStart");
   746         iState = EStateWaitSyncStart;
   793         iState = EStateWaitSyncStart;
   747         }
   794         }
   748     else 
   795     else 
   749         {
   796         {
       
   797         NM_COMMENT("CIpsSosAOMBoxLogic: switching state: EStateIdleAndWaitCommands");
   750         iState = EStateIdleAndWaitCommands;
   798         iState = EStateIdleAndWaitCommands;
   751         }
   799         }
   752     
   800     
   753     }
   801     }
   754 
   802