ipsservices/ipssosaoplugin/src/IpsSosAOImapAgent.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
child 18 578830873419
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 imap specified sync logic
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include <cemailaccounts.h>
       
    22 #include <imapcmds.h>
       
    23 #include <miutset.h>
       
    24 #include <impcmtm.h>
       
    25 #include <msvapi.h>
       
    26 #include <AlwaysOnlineManagerCommon.h>
       
    27 //<cmail>
       
    28 #include "CFSMailCommon.h"
       
    29 //</cmail>
       
    30 
       
    31 
       
    32 #include "IpsSosAOImapAgent.h"
       
    33 #include "IpsSosAOImapPopLogic.h"
       
    34 
       
    35 
       
    36 // from settings
       
    37 #include "ipssetdataapi.h"
       
    38 #include "ipssetutilsconsts.h"
       
    39 
       
    40 // from ipsplugin
       
    41 #include "ipsplgimap4populateop.h"
       
    42 #include "ipsplgcommon.h"
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // ----------------------------------------------------------------------------
       
    46 CIpsSosAOImapAgent::CIpsSosAOImapAgent(             
       
    47     CMsvSession& aSession,
       
    48     MIpsSosAOAgentOperationResponse& aOpResponse,
       
    49     TMsvId aServiceId) : CIpsSosAOBaseAgent(), 
       
    50     iSession(aSession), iOpResponse(aOpResponse),
       
    51     iServiceId( aServiceId ),
       
    52     iState( EStateIdle ), iDoNotDisconnect( EFalse )
       
    53     {
       
    54     FUNC_LOG;
       
    55     
       
    56     }
       
    57 
       
    58 // ----------------------------------------------------------------------------
       
    59 // ----------------------------------------------------------------------------
       
    60 CIpsSosAOImapAgent::~CIpsSosAOImapAgent()
       
    61     {
       
    62     FUNC_LOG;
       
    63     Cancel();
       
    64     SignalSyncCompleted( iServiceId, iError );
       
    65     ClearSignaledFlags();
       
    66     delete iImapSettings;
       
    67     delete iOngoingOp;
       
    68     delete iImapClientMtm;
       
    69     delete iMtmReg;
       
    70     delete iDataApi;
       
    71     iFoldersArray.Close();
       
    72 
       
    73     }
       
    74 
       
    75 // ----------------------------------------------------------------------------
       
    76 // ----------------------------------------------------------------------------
       
    77 CIpsSosAOImapAgent* CIpsSosAOImapAgent::NewL( 
       
    78     CMsvSession& aSession,
       
    79     MIpsSosAOAgentOperationResponse& aOpResponse,
       
    80     TMsvId aServiceId )
       
    81     {
       
    82     FUNC_LOG;
       
    83     CIpsSosAOImapAgent* self = new (ELeave) CIpsSosAOImapAgent( 
       
    84             aSession,  aOpResponse, aServiceId );
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL();
       
    87     CleanupStack::Pop( self );
       
    88     return self;
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // ----------------------------------------------------------------------------
       
    93 void CIpsSosAOImapAgent::ConstructL()
       
    94     {
       
    95     FUNC_LOG;
       
    96     CActiveScheduler::Add(this);
       
    97     
       
    98     iMtmReg = CClientMtmRegistry::NewL( iSession );
       
    99     CBaseMtm* bmtm = iMtmReg->NewMtmL( KUidMsgTypeIMAP4 );
       
   100     iImapClientMtm = static_cast<CImap4ClientMtm*>(bmtm);
       
   101     iDataApi = CIpsSetDataApi::NewL( iSession );
       
   102     iState = EStateIdle; 
       
   103     }
       
   104  
       
   105 // ----------------------------------------------------------------------------
       
   106 // ----------------------------------------------------------------------------
       
   107 void CIpsSosAOImapAgent::DoCancel()
       
   108     {
       
   109     FUNC_LOG;
       
   110     if (iOngoingOp )
       
   111         {
       
   112         iOngoingOp->Cancel();
       
   113         }
       
   114     delete iOngoingOp;
       
   115     iOngoingOp = NULL;
       
   116     
       
   117     SignalSyncCompleted( iServiceId, iError );
       
   118     ClearSignaledFlags();
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // ----------------------------------------------------------------------------
       
   123 void CIpsSosAOImapAgent::HandleImapConnectionEvent(
       
   124     TImapConnectionEvent aConnectionEvent )
       
   125     {
       
   126     FUNC_LOG;
       
   127     switch ( aConnectionEvent )
       
   128         {
       
   129         case EConnectingToServer:
       
   130             break;
       
   131         case ESynchronisingFolderList:
       
   132         case ESynchronisingInbox:
       
   133         case ESynchronisingFolders:
       
   134             SignalSyncStarted( iServiceId );
       
   135             break;
       
   136         case ESynchronisationComplete:
       
   137             break;
       
   138         case EDisconnecting:
       
   139             break;
       
   140         case EConnectionCompleted:
       
   141         default:
       
   142             break;
       
   143         }
       
   144     }
       
   145 
       
   146 // ----------------------------------------------------------------------------
       
   147 // ----------------------------------------------------------------------------
       
   148 void CIpsSosAOImapAgent::RequestResponseL( 
       
   149         TFSProgress /*aEvent*/, 
       
   150         TInt /*aRequestId*/ )
       
   151     {
       
   152     FUNC_LOG;
       
   153     
       
   154     }
       
   155 
       
   156 // ----------------------------------------------------------------------------
       
   157 // ----------------------------------------------------------------------------
       
   158 void CIpsSosAOImapAgent::RunL()
       
   159     {
       
   160     FUNC_LOG;
       
   161     if ( iOngoingOp )
       
   162         {
       
   163         // errors are not tolerated atm
       
   164         if (  iOngoingOp->iStatus.Int() != KErrNone )
       
   165             {
       
   166             iError = iOngoingOp->iStatus.Int();
       
   167             SignalSyncCompleted( iServiceId, iError );
       
   168             CancelAllAndDisconnectL();
       
   169             }
       
   170         delete iOngoingOp;
       
   171         iOngoingOp = NULL;
       
   172         }
       
   173     
       
   174     switch( iState )
       
   175          {
       
   176          case EStateConnectAndSync:
       
   177              iState = EStateRefreshFolderArray;
       
   178              SetActiveAndCompleteThis();
       
   179              break;
       
   180          case EStateConnectAndSyncOnHold:
       
   181              StartSyncL();
       
   182              break;
       
   183          case EStateRefreshFolderArray:
       
   184              iDataApi->GetSubscribedImapFoldersL( iServiceId , iFoldersArray );
       
   185              iState = EStatePopulateAll;
       
   186              SetActiveAndCompleteThis();
       
   187              break;
       
   188          case EStatePopulateAll:
       
   189              PopulateAllL();
       
   190              break;
       
   191          case EStatePopulateOnHold:
       
   192              PopulateAllL();
       
   193              break;
       
   194          case EStateFetchOngoing:
       
   195              break;
       
   196          case EStateFetchOnHold:
       
   197              break;
       
   198          case EStateDisconnect:
       
   199              if ( !iDoNotDisconnect )
       
   200                  {
       
   201                  CancelAllAndDisconnectL();
       
   202                  }
       
   203              else
       
   204                  {
       
   205                  iState = EStateCompleted;
       
   206                  SetActiveAndCompleteThis();
       
   207                  }
       
   208              break;
       
   209          case EStateCompleted:
       
   210              TRAP_IGNORE( iOpResponse.OperationCompletedL( iError ) );
       
   211              SignalSyncCompleted( iServiceId, iError );
       
   212              iError = KErrNone;
       
   213              ClearSignaledFlags();
       
   214              iState = EStateIdle;
       
   215              break;
       
   216          case EStateIdle:
       
   217              break;
       
   218          default:
       
   219          break;
       
   220   
       
   221          }
       
   222     }
       
   223 
       
   224 // ----------------------------------------------------------------------------
       
   225 // ----------------------------------------------------------------------------
       
   226 TInt CIpsSosAOImapAgent::RunError( TInt aError )
       
   227     {
       
   228     FUNC_LOG;
       
   229     iError = aError;
       
   230     if ( IsConnected() && iState != EStateDisconnect )
       
   231         {
       
   232         TRAP_IGNORE( CancelAllAndDisconnectL() );
       
   233         }
       
   234     else if ( IsConnected() && iState == EStateDisconnect )
       
   235         {
       
   236         iSession.StopService( iServiceId );
       
   237         iState = EStateCompleted;
       
   238         SetActiveAndCompleteThis();
       
   239         }
       
   240     else
       
   241         {
       
   242         iState = EStateCompleted;
       
   243         iError = KErrCancel;
       
   244         SetActiveAndCompleteThis();
       
   245         }
       
   246     return KErrNone;
       
   247     }
       
   248 
       
   249 // ----------------------------------------------------------------------------
       
   250 // ----------------------------------------------------------------------------
       
   251 void CIpsSosAOImapAgent::GetServerAddress( 
       
   252         TBuf<KIpsSosAOTextBufferSize>& aIncomingServer ) const
       
   253     {
       
   254     FUNC_LOG;
       
   255     if ( !iImapSettings )
       
   256         {
       
   257         return;
       
   258         }
       
   259     TPtrC addr = iImapSettings->ServerAddress();
       
   260     __ASSERT_DEBUG( ( 
       
   261             addr.Length() <= KIpsSosAOTextBufferSize ), 
       
   262             User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   263     
       
   264     if ( addr.Length() <= KIpsSosAOTextBufferSize )
       
   265         {
       
   266         aIncomingServer.Copy( addr );
       
   267         }
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // ----------------------------------------------------------------------------
       
   272 void CIpsSosAOImapAgent::GetUsername( 
       
   273         TBuf8<KIpsSosAOTextBufferSize>& aUsername ) const
       
   274     {
       
   275     FUNC_LOG;
       
   276     if ( !iImapSettings )
       
   277         {
       
   278         return;
       
   279         }
       
   280     TPtrC8 usrn = iImapSettings->LoginName();
       
   281     __ASSERT_DEBUG( ( 
       
   282             usrn.Length() <= KIpsSosAOTextBufferSize ), 
       
   283             User::Panic( KIpsSosAOPanicLit, KErrGeneral) );
       
   284     
       
   285     if ( usrn.Length() <= KIpsSosAOTextBufferSize )
       
   286         {
       
   287         aUsername.Copy( usrn );
       
   288         }
       
   289     }
       
   290 
       
   291 // ----------------------------------------------------------------------------
       
   292 // ----------------------------------------------------------------------------
       
   293 TBool CIpsSosAOImapAgent::IsConnected() const
       
   294     {
       
   295     FUNC_LOG;
       
   296     TMsvEntry tentry;
       
   297     TMsvId service;
       
   298     iSession.GetEntry( iServiceId, service, tentry );
       
   299     return tentry.Connected();
       
   300     }
       
   301 
       
   302 // ----------------------------------------------------------------------------
       
   303 // ----------------------------------------------------------------------------
       
   304 CIpsSosAOBaseAgent::TAgentState CIpsSosAOImapAgent::GetState() const
       
   305     {
       
   306     FUNC_LOG;
       
   307     return iState;
       
   308     }
       
   309 
       
   310 // ----------------------------------------------------------------------------
       
   311 // ---------------------------------------------------------------------------- 
       
   312 void CIpsSosAOImapAgent::StartSyncL()
       
   313     {
       
   314     FUNC_LOG;
       
   315     if ( iOngoingOp )
       
   316         {
       
   317         User::Leave( KErrNotReady );
       
   318         }
       
   319     LoadSettingsL( );
       
   320     if ( !IsConnected() )
       
   321         {
       
   322         TPckg<MMsvImapConnectionObserver*> parameter(this);
       
   323         // connect and synchronise starts background sync or idle
       
   324         CMsvEntrySelection* sel = new ( ELeave ) CMsvEntrySelection();
       
   325         CleanupStack::PushL( sel );
       
   326         sel->AppendL( iServiceId );
       
   327         iImapClientMtm->SwitchCurrentEntryL( iServiceId );
       
   328         iOngoingOp = iImapClientMtm->InvokeAsyncFunctionL(
       
   329                 KIMAP4MTMConnectAndSyncCompleteAfterFullSync, 
       
   330                 *sel, parameter, iStatus);
       
   331         CleanupStack::PopAndDestroy( sel );
       
   332         SetActive();
       
   333         iState = EStateConnectAndSync;
       
   334         }
       
   335     else
       
   336         {
       
   337         // do not do anything if we are connected, especially do never
       
   338         // try to sync if sync is is already started (ex. from ips plugin)
       
   339         // that cause problems with imap flags etc.
       
   340         iError = KErrNone;
       
   341         iState = EStateCompleted;
       
   342         SetActiveAndCompleteThis();
       
   343         }
       
   344     }
       
   345 
       
   346 // ----------------------------------------------------------------------------
       
   347 // ----------------------------------------------------------------------------
       
   348 void CIpsSosAOImapAgent::StartFetchMessagesL( 
       
   349         const RArray<TMsvId>& /*aFetchMsgArray*/ )
       
   350     {
       
   351     FUNC_LOG;
       
   352     
       
   353     }
       
   354 
       
   355 // ----------------------------------------------------------------------------
       
   356 // ----------------------------------------------------------------------------
       
   357 void CIpsSosAOImapAgent::CancelAllAndDisconnectL()
       
   358     {
       
   359     FUNC_LOG;
       
   360     iDoNotDisconnect = EFalse;
       
   361     iState = EStateCompleted;
       
   362     iFoldersArray.Reset();
       
   363 
       
   364     if ( IsActive() )
       
   365         {
       
   366         Cancel();
       
   367         }
       
   368 
       
   369     if ( IsConnected() )
       
   370         {
       
   371         TBuf8<1> dummy;
       
   372         CMsvEntrySelection* sel = new ( ELeave ) CMsvEntrySelection();
       
   373         CleanupStack::PushL( sel );
       
   374         sel->AppendL( iServiceId );
       
   375         iImapClientMtm->SwitchCurrentEntryL( iServiceId );
       
   376         TRAPD( error, iOngoingOp = iImapClientMtm->InvokeAsyncFunctionL(
       
   377             KIMAP4MTMDisconnect, 
       
   378             *sel, dummy, iStatus) );
       
   379         CleanupStack::PopAndDestroy( sel );
       
   380         
       
   381         if ( error == KErrNone )
       
   382             {
       
   383             SetActive();
       
   384             }
       
   385         else
       
   386             {
       
   387             iSession.StopService( iServiceId );
       
   388             SetActiveAndCompleteThis();
       
   389             }
       
   390         }
       
   391     else
       
   392         {
       
   393         SetActiveAndCompleteThis();
       
   394         }
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------------------------------
       
   398 // ----------------------------------------------------------------------------
       
   399 void CIpsSosAOImapAgent::CancelAllAndDoNotDisconnect()
       
   400     {
       
   401     FUNC_LOG;
       
   402     iDoNotDisconnect = ETrue;
       
   403     if ( IsActive() )
       
   404         {
       
   405         Cancel();
       
   406         }
       
   407     iFoldersArray.Reset();
       
   408     SignalSyncCompleted( iServiceId, KErrCancel );
       
   409     iState = EStateCompleted;
       
   410     SetActiveAndCompleteThis();
       
   411     }
       
   412 
       
   413 // ----------------------------------------------------------------------------
       
   414 // ----------------------------------------------------------------------------
       
   415 void CIpsSosAOImapAgent::DoNotDisconnect()
       
   416     {
       
   417     FUNC_LOG;
       
   418     iDoNotDisconnect = ETrue;
       
   419     }
       
   420 
       
   421 // ----------------------------------------------------------------------------
       
   422 // ----------------------------------------------------------------------------
       
   423 void CIpsSosAOImapAgent::ClearDoNotDisconnect()
       
   424     {
       
   425     FUNC_LOG;
       
   426     iDoNotDisconnect = EFalse;
       
   427     }
       
   428 
       
   429 // ----------------------------------------------------------------------------
       
   430 // ----------------------------------------------------------------------------
       
   431 void CIpsSosAOImapAgent::HoldOperations()
       
   432     {
       
   433     FUNC_LOG;
       
   434     if ( IsActive() )
       
   435         {
       
   436         Cancel();
       
   437         }
       
   438     
       
   439     if ( iState == EStatePopulateAll )
       
   440         {
       
   441         iState = EStatePopulateOnHold;
       
   442         }
       
   443     else if ( iState == EStateFetchOngoing )
       
   444         {
       
   445         iState = EStateFetchOnHold;
       
   446         }
       
   447     else if ( iState == EStateConnectAndSync )
       
   448         {
       
   449         iState = EStateConnectAndSyncOnHold;
       
   450         }
       
   451     else 
       
   452         {
       
   453         iState = EStateIdle;
       
   454         }
       
   455     }
       
   456 
       
   457 // ----------------------------------------------------------------------------
       
   458 // ----------------------------------------------------------------------------
       
   459 void CIpsSosAOImapAgent::ContinueHoldOperations()
       
   460     {
       
   461     FUNC_LOG;
       
   462     SetActiveAndCompleteThis();
       
   463     }
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // ----------------------------------------------------------------------------
       
   467 void CIpsSosAOImapAgent::PopulateAllL()
       
   468     {
       
   469     FUNC_LOG;
       
   470     TImImap4GetPartialMailInfo info;
       
   471     CIpsSetDataApi::ConstructImapPartialFetchInfo( info, *iImapSettings );
       
   472     
       
   473     if ( !IsConnected() )
       
   474         {
       
   475         SignalSyncCompleted( iServiceId, iError );
       
   476         CancelAllAndDisconnectL();
       
   477         }
       
   478     else if ( iFoldersArray.Count() > 0 && info.iTotalSizeLimit 
       
   479             != KIpsSetDataHeadersOnly )
       
   480          {
       
   481 
       
   482          // only inbox is set, do we have to populate other folders also
       
   483          TMsvId id = iFoldersArray[0];
       
   484          CMsvEntry* cEntry = iSession.GetEntryL( id );
       
   485          CleanupStack::PushL( cEntry );
       
   486          CMsvEntrySelection* sel = cEntry->ChildrenWithTypeL( 
       
   487                  KUidMsvMessageEntry ); 
       
   488          CleanupStack::PushL( sel );
       
   489          
       
   490          info.iDestinationFolder = iFoldersArray[0];
       
   491          
       
   492          CIpsPlgTimerOperation* dummy = NULL;
       
   493          iImapClientMtm->SwitchCurrentEntryL( iServiceId );
       
   494          TFSMailMsgId mbox( KIpsPlgImap4PluginUidValue, iServiceId );
       
   495          iStatus = KRequestPending;
       
   496          iOngoingOp = CIpsPlgImap4PopulateOp::NewL(
       
   497                  iSession,
       
   498                  this->iStatus,
       
   499                  CActive::EPriorityLow,
       
   500                  iServiceId,
       
   501                  *dummy,
       
   502                  info,
       
   503                  *sel,
       
   504                  mbox,
       
   505                  *this,
       
   506                  0,
       
   507                  NULL );
       
   508          
       
   509          iFoldersArray.Remove( 0 );
       
   510          SetActive();
       
   511          iState = EStatePopulateAll;
       
   512          CleanupStack::PopAndDestroy( 2, cEntry );
       
   513          
       
   514          }
       
   515      else
       
   516          {
       
   517          iState = EStateDisconnect;
       
   518          SignalSyncStarted( iServiceId );
       
   519          SetActiveAndCompleteThis();
       
   520          }
       
   521     }
       
   522 
       
   523 // ----------------------------------------------------------------------------
       
   524 // ----------------------------------------------------------------------------
       
   525 void CIpsSosAOImapAgent::LoadSettingsL( )
       
   526     {
       
   527     FUNC_LOG;
       
   528     delete iImapSettings;
       
   529     iImapSettings = NULL;
       
   530     iImapSettings = new ( ELeave ) CImImap4Settings();
       
   531     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   532     TImapAccount imapAcc;
       
   533     accounts->GetImapAccountL(iServiceId, imapAcc );
       
   534     accounts->LoadImapSettingsL( imapAcc, *iImapSettings );
       
   535     CleanupStack::PopAndDestroy( accounts );
       
   536     }
       
   537 
       
   538 // ----------------------------------------------------------------------------
       
   539 // inline
       
   540 // ----------------------------------------------------------------------------    
       
   541 void CIpsSosAOImapAgent::SetActiveAndCompleteThis()
       
   542     {
       
   543     FUNC_LOG;
       
   544     if ( !IsActive() )
       
   545         {
       
   546         SetActive();
       
   547         }
       
   548     TRequestStatus* status = &iStatus;
       
   549     User::RequestComplete(status, KErrNone);
       
   550     }