bluetoothengine/btnotif/btdevicedialogplugin/src/btdevicedialogquerywidget.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 14 May 2010 16:01:46 +0300
changeset 19 43824b19ee35
child 40 997690c3397a
permissions -rw-r--r--
Revision: 201017 Kit: 201019

/*
* 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:  BtDeviceDialogWidget class implementation.
*
*/


#include "btdevicedialogquerywidget.h"
#include "btdevicedialogplugintrace.h"
#include <bluetoothdevicedialogs.h>
#include <hbaction.h>
#include <hbdialog.h>
#include "btdevicedialogpluginerrors.h"

/*!
    class Constructor
 */
BtDeviceDialogQueryWidget::BtDeviceDialogQueryWidget(
        HbMessageBox::MessageBoxType type, const QVariantMap &parameters)
{
    TRACE_ENTRY
    // set properties
    mLastError = NoError;
    mShowEventReceived = false;
    mMessageBox = new HbMessageBox(type);
    
    resetProperties();
    constructQueryDialog(parameters);
    TRACE_EXIT
}

/*!
    Set parameters, implementation of interface
    Invoked when HbDeviceDialog::update calls.
 */
bool BtDeviceDialogQueryWidget::setDeviceDialogParameters(
    const QVariantMap &parameters)
{
    TRACE_ENTRY
    mLastError = NoError;
    processParam(parameters);
    TRACE_EXIT
    return true;
}

/*!
    Get error, implementation of interface
 */
int BtDeviceDialogQueryWidget::deviceDialogError() const
{
    TRACE_ENTRY
    TRACE_EXIT
    return mLastError;
}

/*!
    Close notification, implementation of interface
 */ 
void BtDeviceDialogQueryWidget::closeDeviceDialog(bool byClient)
{
    TRACE_ENTRY
    Q_UNUSED(byClient);
    // Closed by client or internally by server -> no action to be transmitted.
    mSendAction = false;
    mMessageBox->close();
    // If show event has been received, close is signalled from hide event. If not,
    // hide event does not come and close is signalled from here.
    if (!mShowEventReceived) {
        emit deviceDialogClosed();
    }
    TRACE_EXIT
}

/*!
    Return display widget, implementation of interface
 */
HbDialog *BtDeviceDialogQueryWidget::deviceDialogWidget() const
{
    TRACE_ENTRY
    TRACE_EXIT
    return mMessageBox;
}

QObject *BtDeviceDialogQueryWidget::signalSender() const
{
    return const_cast<BtDeviceDialogQueryWidget*>(this);
}        

/*!
    Construct display widget
 */
bool BtDeviceDialogQueryWidget::constructQueryDialog(const QVariantMap &parameters)
{
    TRACE_ENTRY
    // analyze the parameters to compose the properties of the message box widget 
    processParam(parameters);
 
    connect(mMessageBox, SIGNAL(finished(HbAction*)), this, SLOT(messageBoxClosed(HbAction*)));
    
    TRACE_EXIT
    return true;
}

/*!
    Take parameter values and generate relevant property of this widget
 */
void BtDeviceDialogQueryWidget::processParam(const QVariantMap &parameters)
{
    TRACE_ENTRY
    QString keyStr, prompt;
    keyStr.setNum( TBluetoothDialogParams::EResource );
    // Validate if the resource item exists.
    QVariantMap::const_iterator i = parameters.constFind( keyStr );
    // item of ResourceId is not found, can't continue.
    if ( i == parameters.constEnd() ) {
        mLastError = UnknownDeviceDialogError;
        return;
    }

    QVariant param = parameters.value( keyStr );
    int key = param.toInt();
    switch ( key ) {
        // Query dialogs:
        case EAuthorization:
            prompt = QString( tr( "Accept connection request from:\n%1" ) );
            break;
        case EIncomingPairing:
            prompt = QString( tr( "Device '%1' is trying to pair with you. Allow pairing?" ) );
            break;
        case ENumericComparison:
            prompt = QString( tr( "Does this code match the one on %1?\n\n%2" ) );
            break;
        case EPasskeyDisplay:
            prompt = QString( tr( "Enter on %1:\n\n%2" ) );
            break;
        case ESetTrusted:
            prompt = QString( tr( "Authorise this device to make connections automatically?" ) );
            break;
        case EBlockUnpairedDevice:
            prompt = QString( tr( "Do you want to block all future connection attempts from device %1?" ) );
            break;
        case EBlockPairedDevice:
            prompt = QString( tr( "Do you want to block all future connection attempts from paired device %1? \nThis will delete your pairing with the device." ) );
            break;
        // Note dialogs, but not Notification dialogs
        // Input dialogs
        case EPinInput:
        case EObexPasskeyInput:
        // NULL parameters
        case ENoResource:
        case EUnusedResource:
        default:
            mLastError = ParameterError;
            break;
    }
    // Could use QChar with ReplacementCharacter?
    int repls = prompt.count( QString( "%" ) );
    if ( repls > 0 ) {
        QVariant name = parameters.value( QString::number( TBluetoothDeviceDialog::EDeviceName ) );
        prompt = prompt.arg( name.toString() );
        if ( repls > 1 ) {
            QVariant addval = parameters.value( QString::number( TBluetoothDeviceDialog::EAdditionalDesc ) );
            prompt = prompt.arg( addval.toString() );
        }
    }
    // set property value to this dialog widget
    mMessageBox->setText( prompt );
    TRACE_EXIT
}

/*!
    Reset properties to default values
 */
void BtDeviceDialogQueryWidget::resetProperties()
{
    TRACE_ENTRY
    // set to default values
    mMessageBox->setModal(true);
    mMessageBox->setTimeout(HbDialog::NoTimeout);
    mMessageBox->setDismissPolicy(HbDialog::NoDismiss);
    mSendAction = true;
    TRACE_EXIT
    return;
}


void BtDeviceDialogQueryWidget::messageBoxClosed(HbAction* action)
{
    QVariantMap data;
    
    HbMessageBox *dlg=static_cast<HbMessageBox*>(sender());
    if(dlg->actions().first() == action) {
        //Yes
        data.insert( QString( "result" ), QVariant(true));
    } 
    else if(dlg->actions().at(1) == action) {
        //No
        data.insert( QString( "result" ), QVariant(false));
    }
    
    emit deviceDialogData(data);
    emit deviceDialogClosed();
    mSendAction = false;
}