bluetoothengine/btui/btuidelegate/btdelegatevisibility.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 14 May 2010 16:01:46 +0300
changeset 19 43824b19ee35
child 31 a0ea99b6fa53
permissions -rw-r--r--
Revision: 201017 Kit: 201019

/*
* Copyright (c) 2010 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: Delegate class for setting visibility mode
*
*/


#include "btdelegatevisibility.h"
#include "btuimodel.h"
#include <bluetoothuitrace.h>
#include "btqtconstants.h"
#include <btengsettings.h>

const int MAX_TEMPORARY_VISIBILITY = 60;  // minutes, this value comes from the UI spec

/*!
    Constructor.
 */
BtDelegateVisibility::BtDelegateVisibility( BtuiModel& model, QObject *parent )
    : BtAbstractDelegate( model, parent )
{
    TRAP_IGNORE( mBtengSettings = CBTEngSettings::NewL(this) );
    Q_CHECK_PTR( mBtengSettings );
    mOperationOngoing = false;
}

/*!
    Destructor.
 */
BtDelegateVisibility::~BtDelegateVisibility()
{
    delete mBtengSettings;
}
/*!
 * executes visibility delegate functionality, ie. calls CBTEngSettings to set the visibility mode;
 * when operation completes, emits commandCompleted signal
 * Parameters:  Qlist<QVariant> where first item is VisibilityMode integer specifying operation;  
 *              for BtTemporary a 2nd parameter is give which signifies the number of minutes to stay visible.
 */
void BtDelegateVisibility::exec( const QVariant &params )
{
    int minutes, err = 0;

    if (mOperationOngoing) {
        // complete command with error
        emit commandCompleted(KErrInUse);
        return;
    } 
    mOperationOngoing = true;
    
    // read 1st parameter
    BTUI_ASSERT_X(params.toList().at(0).isValid(), "BtDelegateVisibility::exec", "invalid parameter");
    VisibilityMode btQtMode = (VisibilityMode)params.toList().at(0).toInt();
    mOperation = BtEngVisibilityMode(btQtMode);
    
    // verify that we are setting visibility to a new value, otherwise we won't get a callback
    TBTVisibilityMode visibilityMode( EBTVisibilityModeNoScans );
    err = mBtengSettings->GetVisibilityMode( visibilityMode );
    if (err) {
        mOperationOngoing = false;
        emit commandCompleted(err);
        return;
    }
    if (visibilityMode == mOperation) {
        mOperationOngoing = false;
        emit commandCompleted(KErrNone);
        return;
    }
    
    switch (mOperation) {
    case EBTVisibilityModeGeneral :
        err = mBtengSettings->SetVisibilityMode(mOperation, 0);
        break;
    case EBTVisibilityModeHidden:
        err = mBtengSettings->SetVisibilityMode(mOperation, 0);
        break;
    case EBTVisibilityModeTemporary:
        BTUI_ASSERT_X(params.toList().at(1).isValid(), "BtDelegateVisibility::exec", "invalid time parameter");
        minutes = params.toList().at(1).toInt();
        BTUI_ASSERT_X(((minutes >= 0 ) && (minutes <= MAX_TEMPORARY_VISIBILITY)), 
                "BtDelegateVisibility::exec", "invalid time parameter");
        err = mBtengSettings->SetVisibilityMode(mOperation, minutes);
        break;
    default:
        // error
        BTUI_ASSERT_X(false, "BtDelegateVisibility::exec", "invalid parameter");
    }
    if (err) {
        // complete command with error
        mOperationOngoing = false;
        emit commandCompleted(err);
    }
}

void BtDelegateVisibility::PowerStateChanged( TBTPowerStateValue aState )
{
    Q_UNUSED( aState );
}

/*!
 * callback from BtEngine
 *    emits command complete with either: 
 *    1) KErrUnknown if something went wrong, or
 *    2) KErrNone if everything ok 
 */
void BtDelegateVisibility::VisibilityModeChanged( TBTVisibilityMode aState )
{
    if (mOperationOngoing) {
        //Error handling has to be done, if value is not set properly.
        mOperationOngoing = false;
        if (mOperation == aState) {
            emit commandCompleted(KErrNone);
        }
        else {
            emit commandCompleted(KErrUnknown);
        }
    }
}