ipsservices/ipssosplugin/src/ipsplgsyncstatehandler.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 16 Apr 2010 14:51:52 +0300
changeset 18 578830873419
parent 0 8466d47a6819
child 20 ecc8def7944a
permissions -rw-r--r--
Revision: 201011 Kit: 201015

/*
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  class handles mailbox sync state queries
*
*/


#include "emailtrace.h"
#include "ipsplgheaders.h"

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
CIpsPlgSyncStateHandler* CIpsPlgSyncStateHandler::NewL( 
    CMsvSession& aSession, 
    CIpsPlgSosBasePlugin& aPlugin,
    RPointerArray<CIpsPlgSingleOpWatcher>& aOperationsRef )
    {
    FUNC_LOG;
    CIpsPlgSyncStateHandler* self = new( ELeave ) CIpsPlgSyncStateHandler( 
        aSession, aPlugin, aOperationsRef );
    CleanupStack::PushL( self );
    self->ConstructL( );
    CleanupStack::Pop( self );
    return self;
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
CIpsPlgSyncStateHandler::~CIpsPlgSyncStateHandler()
    {
    FUNC_LOG;
    iSyncingMailboxes.Close();
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
CIpsPlgSyncStateHandler::CIpsPlgSyncStateHandler( 
    CMsvSession& aSession, 
    CIpsPlgSosBasePlugin& aPlugin,
    RPointerArray<CIpsPlgSingleOpWatcher>& aOperationsRef ) : 
    iSession( aSession ), iPlugin( aPlugin ), iOperationsRef( aOperationsRef )
    {
    FUNC_LOG;
    
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void CIpsPlgSyncStateHandler::ConstructL()
    {
    FUNC_LOG;
    
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void CIpsPlgSyncStateHandler::HandlePropertyEventL( 
        TInt aEvent, TInt aMailbox, TInt aPluginId ,TInt /*aError*/ )
    {
    FUNC_LOG;
    if ( iPlugin.PluginId() == aPluginId &&
            ( aEvent == KIpsSosEmailSyncStarted || 
            aEvent == KIpsSosEmailSyncCompleted ||
            aEvent == KIpsSosEmailSyncOnHold ) )
        {
        AppendMailboxToSyncingMailbox( aMailbox, aEvent );
        }
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
TSSMailSyncState CIpsPlgSyncStateHandler::GetCurrentSyncState(
    const TFSMailMsgId& aMailboxId )
    {
    FUNC_LOG;
    TMsvEntry tEntry;
    TMsvId service;
    
    TInt err = iSession.GetEntry( aMailboxId.Id(), service, tEntry );

    if ( err != KErrNone || !tEntry.Connected() || iOperationsRef.Count() == 0 )
        {
        // no sync ongoing if not connected
        return Idle;
        }

#ifndef RD_101_EMAIL    
// <cmail> RD_IPS_AO_PLUGIN flaf removed
    RAlwaysOnlineClientSession aosession;
    
    err = aosession.Connect();
    if ( err == KErrNone )
        {
        
        TPckgBuf<TInt> idBuf( aMailboxId.Id() );
        TInt status = KErrNotFound;
        TPckgBuf<TInt> statusBuf( status );
        TInt error = aosession.SendReceiveSync(
            EServerAPIEmailQueryState, idBuf, statusBuf );
        status = statusBuf();
        if ( error == KErrNone && 
                status == EIpsAOPluginStatusSyncStarted )
            {
            return EmailSyncing;
            }
        }
    aosession.Close();
#endif
    
// </cmail> 
   
   // found correct operation
   for ( TInt i = 0; i < iOperationsRef.Count(); i++ )
       {
       const CIpsPlgBaseOperation* baseOp = iOperationsRef[i]->BaseOperation();
       
       if ( baseOp && baseOp->FSMailboxId() == aMailboxId &&
              ( baseOp->IpsOpType() == EIpsOpTypePop3SyncOp
               || baseOp->IpsOpType() == EIpsOpTypeImap4SyncOp
               || baseOp->IpsOpType() == EIpsOpTypeImap4PopulateOp ) )
           {
           // check syncing mailbox, to prevent sync icon running 
           // all the time
           if ( FindMailbox( aMailboxId.Id() ) == KIpsSosEmailSyncCompleted )
               {
               return Idle;
               }
           else
               {
               return EmailSyncing;
               }
           }
       }
    return Idle;
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------    
TInt CIpsPlgSyncStateHandler::FindSyncingMailbox( TMsvId aMailbox )
    {
    FUNC_LOG;
    TInt index = KErrNotFound;
    index = FindMailbox( aMailbox );
    
    if ( index == KErrNotFound )
        {
        TInt ipsState = KErrNotFound;
        TSSMailSyncState state = GetCurrentSyncState(
            TFSMailMsgId( iPlugin.PluginId(), aMailbox ) );
        if ( state == EmailSyncing )
            {
            ipsState = KIpsSosEmailSyncStarted;
            }
        else
            {
            ipsState = KIpsSosEmailSyncCompleted;
            }
        TInt count = iSyncingMailboxes.Append( 
                TIpsMailboxState( aMailbox, ipsState ) );
        index = FindMailbox( aMailbox );
        }

    return index;
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------  
TInt CIpsPlgSyncStateHandler::FindMailbox( TMsvId aMailbox )
    {
    FUNC_LOG;
    TInt index = KErrNotFound;
    for ( TInt i = iSyncingMailboxes.Count()-1; i >= 0; i-- )
        {
        if ( iSyncingMailboxes[i].iMailbox == aMailbox )
            {
            index = i;
            break;
            }
        }
    return index;
    }

// ---------------------------------------------------------------------------
// --------------------------------------------------------------------------- 
void CIpsPlgSyncStateHandler::AppendMailboxToSyncingMailbox( 
        TMsvId aMailbox, TInt aState )
    {
    FUNC_LOG;
    TInt index = FindMailbox( aMailbox );
    if ( index == KErrNotFound )
        {
        iSyncingMailboxes.Append( TIpsMailboxState( aMailbox, aState ) );
        }
    else
        {
        iSyncingMailboxes[index].iState = aState; // faulty CS warning
        }
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
TInt CIpsPlgSyncStateHandler::GetMailboxIpsState( TMsvId aMailbox )
    {
    FUNC_LOG;
    TInt index = FindSyncingMailbox( aMailbox );
    TInt state = KErrNotFound;
    if ( index != KErrNotFound )
        {
        state = iSyncingMailboxes[index].iState; // faulty CS warning
        }
    return state;
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void CIpsPlgSyncStateHandler::NotifyMailboxRemove( TMsvId aMailbox )
    {
    FUNC_LOG;
    TInt syncindex = FindMailbox( aMailbox );
    if ( syncindex != KErrNotFound )
        {
        iSyncingMailboxes.Remove(syncindex);
        }
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
TInt CIpsPlgSyncStateHandler::SolveOpProtocolType( )
    {
    FUNC_LOG;
    TInt ret = EIpsOpTypeUnknown;
    TUint pluginId = iPlugin.PluginId();
    if (  pluginId == IPSSOSIMAP4PLUGIN_IMPLEMENTATION_UID )
        {
        ret = EIpsOpTypeImap4SyncOp;
        }
    else if ( pluginId == IPSSOSPOP3PLUGIN_IMPLEMENTATION_UID )
        {
        ret = EIpsOpTypePop3SyncOp;
        }
    return ret;
    }

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void CIpsPlgSyncStateHandler::SaveSuccessfulSyncTimeL( 
        CMsvSession& /*aSession*/, TMsvId /*aService*/ )
    {
    FUNC_LOG;
    TTime now;
    now.HomeTime();
    //CIpsSetDataExtension* extendedSettings = CIpsSetDataExtension::NewLC();
    //CIpsSetDataApi* dataApi = CIpsSetDataApi::NewL( aSession );
    //CleanupStack::PushL( dataApi );
    //dataApi->LoadExtendedSettingsL( aService, *extendedSettings );
    //TAOInfo info;
    //info.iLastSuccessfulUpdate = now;
    //info.iUpdateSuccessfulWithCurSettings = ETrue;
    //extendedSettings->SetLastUpdateInfo( info );
    // clear flag
    //extendedSettings->SetEmnReceivedButNotSyncedFlag( EFalse );
    //dataApi->SaveExtendedSettingsL( *extendedSettings );
    //CleanupStack::PopAndDestroy( 2, extendedSettings );
    }


// End of file