radioengine/utils/src/cradiopropertyobserver.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 16 Apr 2010 14:58:55 +0300
changeset 14 63aabac4416d
parent 13 46974bebc798
child 28 075425b8d9a4
permissions -rw-r--r--
Revision: 201011 Kit: 201015

/*
* Copyright (c) 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:
*
*/

#include "cradioenginelogger.h"

#include "cradiopropertyobserver.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CRadioPropertyObserver::CRadioPropertyObserver( MRadioPropertyChangeObserver& aObserver,
                                                const TUid& aCategory,
                                                const TUint aKey,
                                                const TRadioPropertyType aPropertyType )
    : CActive( CActive::EPriorityStandard )
    , iObserver( aObserver )
    , iCategory( aCategory )
    , iKey( aKey )
    , iPropertyType( aPropertyType )
    {
    }

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRadioPropertyObserver::ConstructL()
    {
    switch ( iPropertyType )
        {
        case ERadioPropertyInt:
            {
            break;
            }
        case ERadioPropertyByteArray:
            {
            iValueByteArray = HBufC8::NewL( RProperty::KMaxPropertySize );
            break;
            }
        case ERadioPropertyText:
            {
            // Max size in bytes, length is size / 2
            iValueText = HBufC::NewL( RProperty::KMaxPropertySize / 2 );
            break;
            }
        default:
            {
            break;
            }
        }

    User::LeaveIfError( iProperty.Attach( iCategory, iKey ) );
    CActiveScheduler::Add( this );
    }

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
EXPORT_C CRadioPropertyObserver* CRadioPropertyObserver::NewL( MRadioPropertyChangeObserver& aObserver,
                                                               const TUid& aCategory,
                                                               const TUint aKey,
                                                               const TRadioPropertyType aPropertyType )
    {
    CRadioPropertyObserver* self = new( ELeave )CRadioPropertyObserver( aObserver,
                                            aCategory, aKey, aPropertyType );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CRadioPropertyObserver::~CRadioPropertyObserver()
    {
    Cancel();
    iProperty.Close();
    delete iValueByteArray;
    delete iValueText;
    }

// ---------------------------------------------------------------------------
// Subscribes to a property and reads the value, if not already active.
// ---------------------------------------------------------------------------
//
EXPORT_C void CRadioPropertyObserver::ActivateL()
    {
    if ( !IsActive() )
        {
        RunL();
        }
    }

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRadioPropertyObserver::RunL()
    {
    LOG_METHOD_AUTO;

    iProperty.Subscribe( iStatus );
    SetActive();

    TInt err( KErrNone );

    switch ( iPropertyType )
        {
        case ERadioPropertyInt:
            {
            err = iProperty.Get( iValueInt );
            if ( !err )
                {
                iObserver.HandlePropertyChangeL( iCategory, iKey, iValueInt );
                }
            break;
            }
        case ERadioPropertyByteArray:
            {
            TPtr8 ptr8( iValueByteArray->Des() );
            err = iProperty.Get( ptr8 );
            if ( !err )
                {
                iObserver.HandlePropertyChangeL( iCategory, iKey, *iValueByteArray );
                }
            break;
            }
        case ERadioPropertyText:
            {
            TPtr ptr( iValueText->Des() );
            err = iProperty.Get( ptr );
            if ( !err )
                {
                iObserver.HandlePropertyChangeL( iCategory, iKey, *iValueText );
                }
            break;
            }
        default:
            {
            break;
            }
        }

    if ( err )
        {
        iObserver.HandlePropertyChangeErrorL( iCategory, iKey, err );
        }
    }

// -----------------------------------------------------------------------------
// Cancels an outstanding active request
// -----------------------------------------------------------------------------
//
void CRadioPropertyObserver::DoCancel()
    {
    iProperty.Cancel();
    }

// -----------------------------------------------------------------------------
// Getter for integer value
// -----------------------------------------------------------------------------
//
EXPORT_C TInt CRadioPropertyObserver::ValueInt() const
    {
    return iValueInt;
    }

// -----------------------------------------------------------------------------
// Getter for byte array value
// -----------------------------------------------------------------------------
//
EXPORT_C const TDesC8& CRadioPropertyObserver::ValueDes8() const
    {
    return *iValueByteArray;
    }

// -----------------------------------------------------------------------------
// Getter for text value
// -----------------------------------------------------------------------------
//
EXPORT_C const TDesC& CRadioPropertyObserver::ValueDes() const
    {
    return *iValueText;
    }