bearermanagement/mpm/src/mpmdatausagewatcher.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Thu, 19 Aug 2010 10:18:49 +0300
branchRCL_3
changeset 55 fc7b30ed2058
parent 17 c14618f9de99
child 57 05bc53fe583b
permissions -rw-r--r--
Revision: 201030 Kit: 201033

/*
 * Copyright (c) 2008-2009 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: Listen cellular data usage key changes in central repository.
 *
 */

#include <e32base.h>
#include <centralrepository.h>
#include <cmmanagerkeys.h>
#include <cmgenconnsettings.h>

#include "mpmlogger.h"
#include "mpmserver.h"
#include "mpmdatausagewatcher.h"

// ---------------------------------------------------------------------------
// Default C++ constructor.
// ---------------------------------------------------------------------------
//
CMpmDataUsageWatcher::CMpmDataUsageWatcher( CMPMServer* aServer ) :
    CActive( EPriorityStandard ), iServer( aServer )
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::CMpmDataUsageWatcher" )

    CActiveScheduler::Add( this );
    }

// ---------------------------------------------------------------------------
// Symbian 2nd phase constructor. Creates a central repository object.
// ---------------------------------------------------------------------------
//
void CMpmDataUsageWatcher::ConstructL()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::ConstructL" )

    iRepository = CRepository::NewL( KCRUidCmManager );
    }

// ---------------------------------------------------------------------------
// Creates a new object by calling the two-phased constructor.
// ---------------------------------------------------------------------------
//
CMpmDataUsageWatcher* CMpmDataUsageWatcher::NewL( CMPMServer* aServer )
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::NewL" )

    CMpmDataUsageWatcher* self = new( ELeave ) CMpmDataUsageWatcher( aServer );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop( self );
    return self;
    }

// ---------------------------------------------------------------------------
// Destructor.
// ---------------------------------------------------------------------------
//
CMpmDataUsageWatcher::~CMpmDataUsageWatcher()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::~CMpmDataUsageWatcher" )

    Cancel();
    delete iRepository;
    }

// ---------------------------------------------------------------------------
// Order notification from changes.
// ---------------------------------------------------------------------------
//
void CMpmDataUsageWatcher::StartL()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::StartL" )

    // Get the initial data usage value from repository.
    User::LeaveIfError( GetCurrentDataUsageValue() );

    // Request notifications.
    User::LeaveIfError( RequestNotifications() );
    }

// ---------------------------------------------------------------------------
// From class CActive.
// Event is received when there is a change in central repository key.
// ---------------------------------------------------------------------------
//
void CMpmDataUsageWatcher::RunL()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::RunL" )

    if ( iStatus.Int() < KErrNone )
        {
        MPMLOGSTRING2("Status: 0x%08X", iStatus.Int())
        iErrorCounter++;
        if ( iErrorCounter > KMpmDataUsageWatcherCenRepErrorThreshold )
            {
            MPMLOGSTRING2("Over %d consecutive errors, stopping notifications permanently.",
                    KMpmDataUsageWatcherCenRepErrorThreshold)
            return;
            }
        // Else: Error occured but counter not expired. Proceed.
        }
    else
        {
        // Notification is received ok => Reset the counter.
        iErrorCounter = 0;

        // Get the new Cellular data usage setting value from central repository.
        TInt oldCellularDataUsage = iCellularDataUsage;

        if ( GetCurrentDataUsageValue() == KErrNone // Updates iCellularDataUsage
             && oldCellularDataUsage != iCellularDataUsage
             && iServer->RoamingWatcher()->RoamingStatus() != EMPMRoamingStatusUnknown )
            {
            // Setting changed while cellular is in use

            if ( iCellularDataUsage == ECmCellularDataUsageDisabled )
                {
                // Cellular data usage disabled -> disconnect cellular
                iServer->StopCellularConns();
                }
            else if ( iCellularDataUsage == ECmCellularDataUsageConfirm )
                {
                // Cellular data usage needs to be confirmed -> disconnect if 
                // there are only silent cellular connections left.
                iServer->StopCellularConns( ETrue );
                }

            }
        }
    
    RequestNotifications();
    }

// ---------------------------------------------------------------------------
// From class CActive.
// Cancel outstanding request.
// ---------------------------------------------------------------------------
//
void CMpmDataUsageWatcher::DoCancel()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::DoCancel" )

    iRepository->NotifyCancel( KCurrentCellularDataUsage );
    }

// ---------------------------------------------------------------------------
// Request notifications.
// ---------------------------------------------------------------------------
//
TInt CMpmDataUsageWatcher::RequestNotifications()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::RequestNotifications" )

    TInt err = iRepository->NotifyRequest( KCurrentCellularDataUsage, iStatus );
        
    if ( err == KErrNone )
        {
        SetActive();
        }
    else
        {
        MPMLOGSTRING2( "CMpmDataUsageWatcher::RequestNotifications, ERROR: %d", err )
        }
    return err;
    }

// ---------------------------------------------------------------------------
// Get current repository key value.
// ---------------------------------------------------------------------------
//
TInt CMpmDataUsageWatcher::GetCurrentDataUsageValue()
    {
    MPMLOGSTRING( "CMpmDataUsageWatcher::GetCurrentDataUsageValue" )

    TInt err = iRepository->Get( KCurrentCellularDataUsage, iCellularDataUsage );
        
    if ( err != KErrNone )
        {
        MPMLOGSTRING2( "CMpmDataUsageWatcher::GetCurrentDataUsageValue, ERROR: %d", err )
        }
    return err;
    }

// -----------------------------------------------------------------------------
// CMpmDataUsageWatcher::CellularDataUsage
// -----------------------------------------------------------------------------
//
TInt CMpmDataUsageWatcher::CellularDataUsage() const
    {
    return iCellularDataUsage;
    }