bluetoothengine/btui/btuidelegate/btdelegatepair.cpp
branchRCL_3
changeset 55 613943a21004
equal deleted inserted replaced
54:0ba996a9b75d 55:613943a21004
       
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "btdelegatepair.h"
       
    19 #include "btuiutil.h"
       
    20 #include "btdelegatefactory.h"
       
    21 #include "btqtconstants.h"
       
    22 
       
    23 #include <QModelIndex>
       
    24 #include <btsettingmodel.h>
       
    25 #include <btdevicemodel.h>
       
    26 #include <bluetoothuitrace.h>
       
    27 #include <hbnotificationdialog.h>
       
    28 #include <hblabel.h>
       
    29 #include <hbprogressbar.h>
       
    30 
       
    31 // docml to load
       
    32 const char* BTUI_PAIR_WAIT_DOCML = ":/docml/pairwaitingdialog.docml";
       
    33 
       
    34 BtDelegatePair::BtDelegatePair(
       
    35         BtSettingModel* settingModel, 
       
    36         BtDeviceModel* deviceModel, 
       
    37         QObject *parent) :
       
    38     BtAbstractDelegate(settingModel, deviceModel, parent), mBtengConnMan(0)
       
    39 {
       
    40     mLoader = new HbDocumentLoader();
       
    41     mAbstractDelegate = NULL;
       
    42 }
       
    43 
       
    44 BtDelegatePair::~BtDelegatePair()
       
    45 {
       
    46     delete mBtengConnMan;
       
    47     delete mLoader;
       
    48 }
       
    49 
       
    50 void BtDelegatePair::exec( const QVariant &params )
       
    51 {
       
    52     
       
    53     deviceIndex = params.value<QModelIndex>();
       
    54     
       
    55     if(isBtPowerOn()) {
       
    56         exec_pair();
       
    57     }
       
    58     else {
       
    59         //If Bt Power is off, switch it on and then perform pairing.
       
    60         //todo: Do we ask for user confirmation here..?
       
    61         if (!mAbstractDelegate) { 
       
    62             mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::ManagePower, 
       
    63                     getSettingModel(), getDeviceModel() ); 
       
    64             connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(powerDelegateCompleted(int)) );
       
    65             mAbstractDelegate->exec(QVariant(BtPowerOn));
       
    66         }
       
    67     }
       
    68     
       
    69 }
       
    70 
       
    71 void BtDelegatePair::powerDelegateCompleted(int error)
       
    72 {
       
    73     if (mAbstractDelegate) {
       
    74         disconnect(mAbstractDelegate);
       
    75         delete mAbstractDelegate;
       
    76         mAbstractDelegate = 0;
       
    77     }
       
    78     if ( error == KErrNone ) {
       
    79         exec_pair();
       
    80     } 
       
    81     else {
       
    82         // error
       
    83         emitCommandComplete(error);
       
    84     }
       
    85 }
       
    86 
       
    87 void BtDelegatePair::exec_pair()
       
    88 {
       
    89     int error = KErrNone;
       
    90     mdeviceName = getDeviceModel()->data(deviceIndex,BtDeviceModel::NameAliasRole).toString();
       
    91     
       
    92     QString strBtAddr = getDeviceModel()->data(deviceIndex,BtDeviceModel::ReadableBdaddrRole).toString();
       
    93     int cod = getDeviceModel()->data(deviceIndex,BtDeviceModel::CoDRole).toInt();
       
    94     
       
    95     if ( ! mBtengConnMan ){
       
    96         TRAP( error, mBtengConnMan = CBTEngConnMan::NewL(this) );
       
    97     }
       
    98     
       
    99     if ( !error ) {
       
   100         TBTDevAddr btEngddr;
       
   101         addrReadbleStringToSymbian( strBtAddr, btEngddr );
       
   102         TBTDeviceClass btEngDeviceClass(cod);
       
   103         error = mBtengConnMan->PairDevice(btEngddr, btEngDeviceClass);
       
   104         launchWaitDialog();
       
   105     }
       
   106     
       
   107     if(error) {
       
   108         emitCommandComplete(error);
       
   109     }
       
   110 
       
   111 }
       
   112 
       
   113 
       
   114 void BtDelegatePair::launchWaitDialog()
       
   115 {
       
   116     QString headingText(hbTrId("txt_bt_title_pairing_with_1"));
       
   117     HbLabel *heading;
       
   118     HbProgressBar* progressBar;
       
   119     
       
   120     bool ok = false;
       
   121     mLoader->load( BTUI_PAIR_WAIT_DOCML, &ok );
       
   122     // Exit if the file format is invalid
       
   123     BTUI_ASSERT_X( ok, "BTUI_PAIR_WAIT_DOCML", "Invalid docml file" );
       
   124 
       
   125     mWaitDialog = qobject_cast<HbDialog *>( mLoader->findWidget( "dialog" ) );
       
   126     BTUI_ASSERT_X( mWaitDialog != 0, "BTUI_PAIR_WAIT_DOCML", "dialog not found" );
       
   127     
       
   128     heading = qobject_cast<HbLabel *>( mLoader->findWidget( "heading" ) );
       
   129     BTUI_ASSERT_X( heading != 0, "BTUI_PAIR_WAIT_DOCML", "heading not found" );
       
   130 
       
   131     progressBar = qobject_cast<HbProgressBar *>( mLoader->findWidget( "progressBar" ) );
       
   132     BTUI_ASSERT_X( progressBar != 0, "BTUI_PAIR_WAIT_DOCML", "progressBar not found" );
       
   133     progressBar->setRange(0,0);
       
   134     
       
   135     heading->setPlainText(headingText.arg(mdeviceName));
       
   136     
       
   137     mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
       
   138     mWaitDialog->setTimeout(HbPopup::NoTimeout);
       
   139 
       
   140     mWaitDialog->show();
       
   141 }
       
   142 
       
   143 void BtDelegatePair::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
       
   144                                    RBTDevAddrArray* aConflicts )
       
   145 {
       
   146     Q_UNUSED(aAddr);
       
   147     Q_UNUSED(aErr);    
       
   148     Q_UNUSED(aConflicts);
       
   149 
       
   150 }
       
   151 
       
   152 void BtDelegatePair::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
       
   153 {
       
   154     Q_UNUSED(aAddr);
       
   155     Q_UNUSED(aErr);    
       
   156 }
       
   157 
       
   158 void BtDelegatePair::PairingComplete( TBTDevAddr& aAddr, TInt aErr )
       
   159 {
       
   160     Q_UNUSED(aAddr);
       
   161     
       
   162     mWaitDialog->close();
       
   163     emitCommandComplete(aErr);
       
   164 }
       
   165 
       
   166 void BtDelegatePair::emitCommandComplete(int error)
       
   167 {
       
   168 
       
   169     emit commandCompleted(error);
       
   170 }
       
   171 
       
   172 void BtDelegatePair::cancel()
       
   173 {
       
   174     if ( mBtengConnMan ) {
       
   175         mBtengConnMan->CancelPairDevice();
       
   176     }
       
   177 }
       
   178 
       
   179 
       
   180