ipsservices/ipssosplugin/src/ipsplgsyncstatehandler.cpp
changeset 0 8466d47a6819
child 11 0396474f30f5
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:  class handles mailbox sync state queries
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "ipsplgheaders.h"
       
    21 
       
    22 // ---------------------------------------------------------------------------
       
    23 // ---------------------------------------------------------------------------
       
    24 CIpsPlgSyncStateHandler* CIpsPlgSyncStateHandler::NewL( 
       
    25     CMsvSession& aSession, 
       
    26     CIpsPlgSosBasePlugin& aPlugin,
       
    27     RPointerArray<CIpsPlgSingleOpWatcher>& aOperationsRef )
       
    28     {
       
    29     FUNC_LOG;
       
    30     CIpsPlgSyncStateHandler* self = new( ELeave ) CIpsPlgSyncStateHandler( 
       
    31         aSession, aPlugin, aOperationsRef );
       
    32     CleanupStack::PushL( self );
       
    33     self->ConstructL( );
       
    34     CleanupStack::Pop( self );
       
    35     return self;
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // ---------------------------------------------------------------------------
       
    40 CIpsPlgSyncStateHandler::~CIpsPlgSyncStateHandler()
       
    41     {
       
    42     FUNC_LOG;
       
    43     iSyncingMailboxes.Close();
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // ---------------------------------------------------------------------------
       
    48 CIpsPlgSyncStateHandler::CIpsPlgSyncStateHandler( 
       
    49     CMsvSession& aSession, 
       
    50     CIpsPlgSosBasePlugin& aPlugin,
       
    51     RPointerArray<CIpsPlgSingleOpWatcher>& aOperationsRef ) : 
       
    52     iSession( aSession ), iPlugin( aPlugin ), iOperationsRef( aOperationsRef )
       
    53     {
       
    54     FUNC_LOG;
       
    55     
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // ---------------------------------------------------------------------------
       
    60 void CIpsPlgSyncStateHandler::ConstructL()
       
    61     {
       
    62     FUNC_LOG;
       
    63     
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // ---------------------------------------------------------------------------
       
    68 void CIpsPlgSyncStateHandler::HandlePropertyEventL( 
       
    69         TInt aEvent, TInt aMailbox, TInt aPluginId ,TInt /*aError*/ )
       
    70     {
       
    71     FUNC_LOG;
       
    72     if ( iPlugin.PluginId() == aPluginId &&
       
    73             ( aEvent == KIpsSosEmailSyncStarted || 
       
    74               aEvent == KIpsSosEmailSyncCompleted ||
       
    75               aEvent == KIpsSosEmailSyncOnHold ) )
       
    76         {
       
    77         AppendMailboxToSyncingMailbox( aMailbox, aEvent );
       
    78         }
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // ---------------------------------------------------------------------------
       
    83 TSSMailSyncState CIpsPlgSyncStateHandler::GetCurrentSyncState(
       
    84     const TFSMailMsgId& aMailboxId )
       
    85     {
       
    86     FUNC_LOG;
       
    87     TMsvEntry tEntry;
       
    88     TMsvId service;
       
    89     
       
    90     TInt err = iSession.GetEntry( aMailboxId.Id(), service, tEntry );
       
    91 
       
    92     if ( err != KErrNone || iOperationsRef.Count() == 0 )
       
    93         {
       
    94         return Idle;
       
    95         }
       
    96 
       
    97     // If the mailbox is not online but it has some connection operation
       
    98     // already running, it means that it will be synchronized when the mailbox
       
    99     // goes online. So we need to check is there any connection operation
       
   100     // ongoing and return StartingSync in that case.
       
   101     if( !tEntry.Connected() )
       
   102         {
       
   103         if( ConnOpRunning( aMailboxId ) )
       
   104             {
       
   105             // Some connection operation already processing, so the sync
       
   106             // will start soon
       
   107             return StartingSync;
       
   108             }
       
   109         else
       
   110             {
       
   111             // no sync ongoing if not connected and no connection operations
       
   112             // started
       
   113             return Idle;
       
   114             }
       
   115         }
       
   116 
       
   117     RAlwaysOnlineClientSession aosession;
       
   118     
       
   119     err = aosession.Connect();
       
   120     if ( err == KErrNone )
       
   121         {
       
   122         
       
   123         TPckgBuf<TInt> idBuf( aMailboxId.Id() );
       
   124         TInt status = KErrNotFound;
       
   125         TPckgBuf<TInt> statusBuf( status );
       
   126         TInt error = aosession.SendReceiveSync(
       
   127             EServerAPIEmailQueryState, idBuf, statusBuf );
       
   128         status = statusBuf();
       
   129         if ( error == KErrNone && 
       
   130                 status == EIpsAOPluginStatusSyncStarted )
       
   131             {
       
   132             return EmailSyncing;
       
   133             }
       
   134         }
       
   135     aosession.Close();
       
   136    
       
   137    // found correct operation
       
   138    for ( TInt i = 0; i < iOperationsRef.Count(); i++ )
       
   139        {
       
   140        const CIpsPlgBaseOperation* baseOp = iOperationsRef[i]->BaseOperation();
       
   141        
       
   142        if ( baseOp && baseOp->FSMailboxId() == aMailboxId &&
       
   143             ( baseOp->IpsOpType() == EIpsOpTypePop3SyncOp ||
       
   144               baseOp->IpsOpType() == EIpsOpTypeImap4SyncOp ||
       
   145               baseOp->IpsOpType() == EIpsOpTypeImap4PopulateOp ) )
       
   146            {
       
   147            // Due to timing problems we might in some rare cases report
       
   148            // EmailSyncing here even if the actual syncing is already
       
   149            // finsihed, because the operation is removed from the array
       
   150            // with an async function call. HandlePropertyEventL events
       
   151            // seem to be even more unreliable (sync start event comes with
       
   152            // big delay), so we can't trust those either. Some kind of
       
   153            // redesign is needed to get this sync state query more reliable.
       
   154            return EmailSyncing;
       
   155            }
       
   156        }
       
   157     return Idle;
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // ---------------------------------------------------------------------------
       
   162 TBool CIpsPlgSyncStateHandler::ConnOpRunning( const TFSMailMsgId& aMailBoxId  )
       
   163     {
       
   164     FUNC_LOG;
       
   165     for ( TInt i = 0; i < iOperationsRef.Count(); i++ )
       
   166        {
       
   167        const CIpsPlgBaseOperation* baseOp = iOperationsRef[i]->BaseOperation();
       
   168 
       
   169        if ( baseOp && baseOp->FSMailboxId() == aMailBoxId &&
       
   170               ( baseOp->IpsOpType() == EIpsOpTypePop3SyncOp
       
   171                || baseOp->IpsOpType() == EIpsOpTypeImap4SyncOp
       
   172                || baseOp->IpsOpType() == EIpsOpTypeOnlineOp
       
   173                || baseOp->IpsOpType() == EIpsOpTypeImap4PopulateOp ) )
       
   174            {
       
   175            // Due to timing problems we might in some rare cases report
       
   176            // EmailSyncing here even if the actual syncing is already
       
   177            // finsihed, because the operation is removed from the array
       
   178            // with an async function call. HandlePropertyEventL events
       
   179            // seem to be even more unreliable (sync start event comes with
       
   180            // big delay), so we can't trust those either. Some kind of
       
   181            // redesign is needed to get this sync state query more reliable.
       
   182            return ETrue;
       
   183            }
       
   184        }
       
   185     return EFalse;
       
   186     }
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // ---------------------------------------------------------------------------    
       
   190 TInt CIpsPlgSyncStateHandler::FindSyncingMailbox( TMsvId aMailbox )
       
   191     {
       
   192     FUNC_LOG;
       
   193     TInt index = KErrNotFound;
       
   194     index = FindMailbox( aMailbox );
       
   195     
       
   196     if ( index == KErrNotFound )
       
   197         {
       
   198         TInt ipsState = KErrNotFound;
       
   199         TSSMailSyncState state = GetCurrentSyncState(
       
   200             TFSMailMsgId( iPlugin.PluginId(), aMailbox ) );
       
   201         if ( state == EmailSyncing )
       
   202             {
       
   203             ipsState = KIpsSosEmailSyncStarted;
       
   204             }
       
   205         else
       
   206             {
       
   207             ipsState = KIpsSosEmailSyncCompleted;
       
   208             }
       
   209         TInt count = iSyncingMailboxes.Append( 
       
   210                 TIpsMailboxState( aMailbox, ipsState ) );
       
   211         index = FindMailbox( aMailbox );
       
   212         }
       
   213 
       
   214     return index;
       
   215     }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // ---------------------------------------------------------------------------  
       
   219 TInt CIpsPlgSyncStateHandler::FindMailbox( TMsvId aMailbox )
       
   220     {
       
   221     FUNC_LOG;
       
   222     TInt index = KErrNotFound;
       
   223     for ( TInt i = iSyncingMailboxes.Count()-1; i >= 0; i-- )
       
   224         {
       
   225         if ( iSyncingMailboxes[i].iMailbox == aMailbox )
       
   226             {
       
   227             index = i;
       
   228             break;
       
   229             }
       
   230         }
       
   231     return index;
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // --------------------------------------------------------------------------- 
       
   236 void CIpsPlgSyncStateHandler::AppendMailboxToSyncingMailbox( 
       
   237         TMsvId aMailbox, TInt aState )
       
   238     {
       
   239     FUNC_LOG;
       
   240     TInt index = FindMailbox( aMailbox );
       
   241     if ( index == KErrNotFound )
       
   242         {
       
   243         iSyncingMailboxes.Append( TIpsMailboxState( aMailbox, aState ) );
       
   244         }
       
   245     else
       
   246         {
       
   247         iSyncingMailboxes[index].iState = aState; // faulty CS warning
       
   248         }
       
   249     }
       
   250 
       
   251 // ---------------------------------------------------------------------------
       
   252 // ---------------------------------------------------------------------------
       
   253 TInt CIpsPlgSyncStateHandler::GetMailboxIpsState( TMsvId aMailbox )
       
   254     {
       
   255     FUNC_LOG;
       
   256     TInt index = FindSyncingMailbox( aMailbox );
       
   257     TInt state = KErrNotFound;
       
   258     if ( index != KErrNotFound )
       
   259         {
       
   260         state = iSyncingMailboxes[index].iState; // faulty CS warning
       
   261         }
       
   262     return state;
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // ---------------------------------------------------------------------------
       
   267 void CIpsPlgSyncStateHandler::NotifyMailboxRemove( TMsvId aMailbox )
       
   268     {
       
   269     FUNC_LOG;
       
   270     TInt syncindex = FindMailbox( aMailbox );
       
   271     if ( syncindex != KErrNotFound )
       
   272         {
       
   273         iSyncingMailboxes.Remove(syncindex);
       
   274         }
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // ---------------------------------------------------------------------------
       
   279 TInt CIpsPlgSyncStateHandler::SolveOpProtocolType( )
       
   280     {
       
   281     FUNC_LOG;
       
   282     TInt ret = EIpsOpTypeUnknown;
       
   283     TUint pluginId = iPlugin.PluginId();
       
   284     if (  pluginId == IPSSOSIMAP4PLUGIN_IMPLEMENTATION_UID )
       
   285         {
       
   286         ret = EIpsOpTypeImap4SyncOp;
       
   287         }
       
   288     else if ( pluginId == IPSSOSPOP3PLUGIN_IMPLEMENTATION_UID )
       
   289         {
       
   290         ret = EIpsOpTypePop3SyncOp;
       
   291         }
       
   292     return ret;
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // ---------------------------------------------------------------------------
       
   297 void CIpsPlgSyncStateHandler::SaveSuccessfulSyncTimeL( 
       
   298         CMsvSession& aSession, TMsvId aService )
       
   299     {
       
   300     FUNC_LOG;
       
   301     TTime now;
       
   302     now.HomeTime();
       
   303     CIpsSetDataExtension* extendedSettings = CIpsSetDataExtension::NewLC();
       
   304     CIpsSetDataApi* dataApi = CIpsSetDataApi::NewL( aSession );
       
   305     CleanupStack::PushL( dataApi );
       
   306     dataApi->LoadExtendedSettingsL( aService, *extendedSettings );
       
   307     TAOInfo info;
       
   308     info.iLastSuccessfulUpdate = now;
       
   309     info.iUpdateSuccessfulWithCurSettings = ETrue;
       
   310     extendedSettings->SetLastUpdateInfo( info );
       
   311     // clear flag
       
   312     extendedSettings->SetEmnReceivedButNotSyncedFlag( EFalse );
       
   313     dataApi->SaveExtendedSettingsL( *extendedSettings );
       
   314     CleanupStack::PopAndDestroy( 2, extendedSettings );
       
   315     }
       
   316 
       
   317 
       
   318 // End of file
       
   319