bluetoothengine/btui/btuidelegate/btdelegatepair.cpp
changeset 42 b72428996822
parent 31 a0ea99b6fa53
child 57 5ebadcda06cb
--- a/bluetoothengine/btui/btuidelegate/btdelegatepair.cpp	Fri May 28 17:03:06 2010 +0300
+++ b/bluetoothengine/btui/btuidelegate/btdelegatepair.cpp	Mon Jul 12 18:51:05 2010 +0300
@@ -17,6 +17,8 @@
 
 #include "btdelegatepair.h"
 #include "btuiutil.h"
+#include "btdelegatefactory.h"
+#include "btqtconstants.h"
 
 #include <QModelIndex>
 #include <btsettingmodel.h>
@@ -36,6 +38,7 @@
     BtAbstractDelegate(settingModel, deviceModel, parent), mBtengConnMan(0)
 {
     mLoader = new HbDocumentLoader();
+    mAbstractDelegate = NULL;
 }
 
 BtDelegatePair::~BtDelegatePair()
@@ -46,13 +49,48 @@
 
 void BtDelegatePair::exec( const QVariant &params )
 {
-    int error = KErrNone;
-    QModelIndex index = params.value<QModelIndex>();
+    
+    deviceIndex = params.value<QModelIndex>();
+    
+    if(isBtPowerOn()) {
+        exec_pair();
+    }
+    else {
+        //If Bt Power is off, switch it on and then perform pairing.
+        //todo: Do we ask for user confirmation here..?
+        if (!mAbstractDelegate) { 
+            mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::ManagePower, 
+                    getSettingModel(), getDeviceModel() ); 
+            connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(powerDelegateCompleted(int)) );
+            mAbstractDelegate->exec(QVariant(BtPowerOn));
+        }
+    }
     
-    mdeviceName = getDeviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
+}
+
+void BtDelegatePair::powerDelegateCompleted(int error)
+{
+    if (mAbstractDelegate) {
+        disconnect(mAbstractDelegate);
+        delete mAbstractDelegate;
+        mAbstractDelegate = 0;
+    }
+    if ( error == KErrNone ) {
+        exec_pair();
+    } 
+    else {
+        // error
+        emitCommandComplete(error);
+    }
+}
+
+void BtDelegatePair::exec_pair()
+{
+    int error = KErrNone;
+    mdeviceName = getDeviceModel()->data(deviceIndex,BtDeviceModel::NameAliasRole).toString();
     
-    QString strBtAddr = getDeviceModel()->data(index,BtDeviceModel::ReadableBdaddrRole).toString();
-    int cod = getDeviceModel()->data(index,BtDeviceModel::CoDRole).toInt();
+    QString strBtAddr = getDeviceModel()->data(deviceIndex,BtDeviceModel::ReadableBdaddrRole).toString();
+    int cod = getDeviceModel()->data(deviceIndex,BtDeviceModel::CoDRole).toInt();
     
     if ( ! mBtengConnMan ){
         TRAP( error, mBtengConnMan = CBTEngConnMan::NewL(this) );
@@ -69,12 +107,13 @@
     if(error) {
         emitCommandComplete(error);
     }
-    
+
 }
 
+
 void BtDelegatePair::launchWaitDialog()
 {
-    QString headingText(hbTrId("Pairing with %1"));
+    QString headingText(hbTrId("txt_bt_title_pairing_with_1"));
     HbLabel *heading;
     HbProgressBar* progressBar;
     
@@ -126,15 +165,6 @@
 
 void BtDelegatePair::emitCommandComplete(int error)
 {
-    QString str(hbTrId("Paired to %1"));
-    QString err(hbTrId("Pairing with %1 Failed"));
-    
-    if(error != KErrNone) {
-        HbNotificationDialog::launchDialog(err.arg(mdeviceName));
-    }
-    else {
-        HbNotificationDialog::launchDialog(str.arg(mdeviceName));
-    }
 
     emit commandCompleted(error);
 }