bluetoothengine/btnotif/btdevicedialogplugin/src/btdevicedialoginputwidget.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  BtDeviceDialogWidget class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "btdevicedialoginputwidget.h"
       
    20 #include "btdevicedialogplugintrace.h"
       
    21 #include <bluetoothdevicedialogs.h>
       
    22 #include <hbaction.h>
       
    23 #include <hbdialog.h>
       
    24 #include <hblabel.h>
       
    25 #include <hbvalidator.h>
       
    26 #include "btdevicedialogpluginerrors.h"
       
    27 
       
    28 const int PASSCODE_MAX_LEN = 16; // from BT specs
       
    29 
       
    30 /*!
       
    31     class Constructor
       
    32  */
       
    33 BtDeviceDialogInputWidget::BtDeviceDialogInputWidget(
       
    34         const QVariantMap &parameters)
       
    35 {
       
    36     TRACE_ENTRY
       
    37     // set properties
       
    38     mLastError = NoError;
       
    39     mShowEventReceived = false;
       
    40     mInputDialog = new HbInputDialog();
       
    41     
       
    42     resetProperties();
       
    43     constructInputDialog(parameters);
       
    44     TRACE_EXIT
       
    45 }
       
    46 
       
    47 /*!
       
    48     Set parameters, implementation of interface
       
    49     Invoked when HbDeviceDialog::update calls.
       
    50  */
       
    51 bool BtDeviceDialogInputWidget::setDeviceDialogParameters(
       
    52     const QVariantMap &parameters)
       
    53 {
       
    54     TRACE_ENTRY
       
    55     mLastError = NoError;
       
    56     processParam(parameters);
       
    57     TRACE_EXIT
       
    58     return true;
       
    59 }
       
    60 
       
    61 /*!
       
    62     Get error, implementation of interface
       
    63  */
       
    64 int BtDeviceDialogInputWidget::deviceDialogError() const
       
    65 {
       
    66     TRACE_ENTRY
       
    67     TRACE_EXIT
       
    68     return mLastError;
       
    69 }
       
    70 
       
    71 /*!
       
    72     Close notification, implementation of interface
       
    73  */ 
       
    74 void BtDeviceDialogInputWidget::closeDeviceDialog(bool byClient)
       
    75 {
       
    76     TRACE_ENTRY
       
    77     Q_UNUSED(byClient);
       
    78     // Closed by client or internally by server -> no action to be transmitted.
       
    79     mSendAction = false;
       
    80     mInputDialog->close();
       
    81     // If show event has been received, close is signalled from hide event. If not,
       
    82     // hide event does not come and close is signalled from here.
       
    83     if (!mShowEventReceived) {
       
    84         emit deviceDialogClosed();
       
    85     }
       
    86     TRACE_EXIT
       
    87 }
       
    88 
       
    89 /*!
       
    90     Return display widget, implementation of interface
       
    91  */
       
    92 HbDialog *BtDeviceDialogInputWidget::deviceDialogWidget() const
       
    93 {
       
    94     TRACE_ENTRY
       
    95     TRACE_EXIT
       
    96     return mInputDialog;
       
    97 }
       
    98 
       
    99 QObject *BtDeviceDialogInputWidget::signalSender() const
       
   100 {
       
   101     return const_cast<BtDeviceDialogInputWidget*>(this);
       
   102 }     
       
   103 
       
   104 /*!
       
   105     Construct display widget
       
   106  */
       
   107 bool BtDeviceDialogInputWidget::constructInputDialog(const QVariantMap &parameters)
       
   108 {
       
   109     TRACE_ENTRY
       
   110     // analyze the parameters to compose the properties of the widget
       
   111     processParam(parameters);
       
   112     connect(mInputDialog, SIGNAL(finished(HbAction*)), this, SLOT(inputClosed(HbAction*)));
       
   113     
       
   114     TRACE_EXIT
       
   115     return true;
       
   116 }
       
   117 
       
   118 /*!
       
   119     Take parameter values and generate relevant property of this widget
       
   120  */
       
   121 void BtDeviceDialogInputWidget::processParam(const QVariantMap &parameters)
       
   122 {
       
   123     TRACE_ENTRY
       
   124 
       
   125     QString keyStr, prompt,title,regExp;
       
   126     QVariant name;
       
   127     keyStr.setNum( TBluetoothDialogParams::EResource );
       
   128     // Validate if the resource item exists.
       
   129     QVariantMap::const_iterator i = parameters.constFind( keyStr );
       
   130     // item of ResourceId is not found, can't continue.
       
   131     if ( i == parameters.constEnd() ) {
       
   132         mLastError = UnknownDeviceDialogError;
       
   133         return;
       
   134     }
       
   135 
       
   136     QVariant param = parameters.value( keyStr );
       
   137     if ( param.toInt() == EPinInput ) {
       
   138         // Numeric value only
       
   139         mInputDialog->setInputMode(HbInputDialog::RealInput);
       
   140         title = QString(hbTrId("txt_bt_title_pairing_with_1"));
       
   141         // todo: Fixe the multiline problem
       
   142         prompt = QString( hbTrId( "txt_bt_dialog_please_enter_the_passcode_for_1" ) );
       
   143     }
       
   144     else {
       
   145         mLastError = ParameterError;
       
   146         return;
       
   147     }
       
   148     
       
   149     // check if minLength of passcode required
       
   150     keyStr.setNum( TBluetoothDeviceDialog::EAdditionalDesc );
       
   151     i = parameters.constFind( keyStr );
       
   152     // Mini Length required, update prompt
       
   153     // ToDo: The validator doesn't seem to work properly 
       
   154     if ( i != parameters.constEnd() ) {
       
   155         // Todo : The string ID for the localization is not available yet
       
   156         // for string : "Enter %1 digit passcode for device %2:"
       
   157         // I'm using the "Enter the passcode for device %1" instead
       
   158         prompt = QString( hbTrId( "txt_bt_dialog_please_enter_the_passcode_for_1" ) );
       
   159         param = parameters.value( keyStr );
       
   160         regExp = tr("^\\d{%1,}$");
       
   161         regExp.arg(param.toString());
       
   162         HbValidator* validator = new HbValidator(mInputDialog->lineEdit());
       
   163         validator->addField(
       
   164                 new QRegExpValidator(
       
   165                         QRegExp(regExp, Qt::CaseInsensitive), validator ),"");
       
   166         mInputDialog->setValidator(validator);
       
   167     }else{
       
   168     // Minimum requirement is to have at least 1 digit
       
   169         regExp = tr("^\\d{1,}$");
       
   170         HbValidator* validator = new HbValidator(mInputDialog->lineEdit());
       
   171         validator->addField(
       
   172                 new QRegExpValidator(
       
   173                         QRegExp(regExp, Qt::CaseInsensitive), validator ),"");
       
   174         mInputDialog->setValidator(validator);    
       
   175     }
       
   176     // replace % with the miniLength and device name
       
   177     int repls = prompt.count( QString( "%" ) );
       
   178     if ( repls > 1 ) {
       
   179         prompt = prompt.arg( param.toString() );
       
   180     }
       
   181     if ( repls > 0 ) {
       
   182         name = parameters.value( QString::number( TBluetoothDeviceDialog::EDeviceName ) );
       
   183         prompt = prompt.arg( name.toString() );
       
   184     }
       
   185     repls = title.count(QString("%"));
       
   186     if(repls > 0){
       
   187         title = title.arg( name.toString() );
       
   188     }
       
   189     mInputDialog->setHeadingWidget(new HbLabel(title));
       
   190     mInputDialog->lineEdit(0)->setMaxLength(PASSCODE_MAX_LEN);
       
   191     mInputDialog->lineEdit(0)->setText(tr("")); // clear the input field
       
   192     mInputDialog->setPromptText(prompt);
       
   193     TRACE_EXIT
       
   194 }
       
   195 
       
   196 /*!
       
   197     Reset properties to default values
       
   198  */
       
   199 void BtDeviceDialogInputWidget::resetProperties()
       
   200 {
       
   201     TRACE_ENTRY
       
   202     // set to default values
       
   203     mInputDialog->setModal(true);
       
   204     mInputDialog->setTimeout(HbDialog::NoTimeout);
       
   205     mInputDialog->setDismissPolicy(HbDialog::NoDismiss);
       
   206     mSendAction = true;
       
   207     // Todo: clean the Validator
       
   208     TRACE_EXIT
       
   209     return;
       
   210 }
       
   211 
       
   212 void BtDeviceDialogInputWidget::inputClosed(HbAction *action)
       
   213 {
       
   214     QVariantMap data;
       
   215     
       
   216     HbInputDialog *dlg=static_cast<HbInputDialog*>(sender());
       
   217     if(dlg->actions().first() == action) {
       
   218         //Ok
       
   219         QVariant result( dlg->value().toString().toUtf8() );
       
   220         data.insert( QString( "result" ), QVariant(true));
       
   221         data.insert( QString( "input" ), result );
       
   222    } 
       
   223     else if(dlg->actions().at(1) == action) {
       
   224         //Cancel
       
   225         data.insert( QString( "result" ), QVariant(false));
       
   226     }
       
   227 
       
   228     emit deviceDialogData(data);
       
   229     emit deviceDialogClosed();
       
   230     mSendAction = false;
       
   231 }
       
   232 
       
   233