bluetoothengine/btui/btuidelegate/btdelegatedisconnect.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     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 #include "btdelegatedisconnect.h"
       
    18 #include "btuiutil.h"
       
    19 #include "btuiiconutil.h"
       
    20 #include <QModelIndex>
       
    21 #include <hblabel.h>
       
    22 #include <btsettingmodel.h>
       
    23 #include <btdevicemodel.h>
       
    24 #include <hbnotificationdialog.h>
       
    25 
       
    26 BtDelegateDisconnect::BtDelegateDisconnect(            
       
    27         BtSettingModel* settingModel, 
       
    28         BtDeviceModel* deviceModel, QObject *parent) :
       
    29     BtAbstractDelegate(settingModel, deviceModel, parent), mBtengConnMan(0), mPhyLinks(0),
       
    30 	 mMajorRole(0), mActiveHandling(false), mAddrArrayIndex(0), mDisconOpt(DisconUnknown)
       
    31 {
       
    32     
       
    33 }
       
    34 
       
    35 BtDelegateDisconnect::~BtDelegateDisconnect()
       
    36 {
       
    37     delete mBtengConnMan;
       
    38     delete mPhyLinks;
       
    39     mSocketServ.Close();
       
    40 }
       
    41 
       
    42 void BtDelegateDisconnect::exec( const QVariant &params )
       
    43 {
       
    44     int err;
       
    45     if ( ! mBtengConnMan ){
       
    46         TRAP( err, mBtengConnMan = CBTEngConnMan::NewL(this) );
       
    47     }
       
    48     if(err) {
       
    49         emit commandCompleted(err);
       
    50         return;
       
    51     }
       
    52     if (params.canConvert<int>()){
       
    53         mDisconOpt = (DisconnectOption)params.toInt();
       
    54         mActiveHandling = true;
       
    55         
       
    56         if (mDisconOpt == AllOngoingConnections){
       
    57             err = mBtengConnMan->GetConnectedAddresses(mDevAddrArray);
       
    58             disconnectAllConnections_service();
       
    59         }
       
    60         if(err) {
       
    61             emit commandCompleted(err);
       
    62         }
       
    63     }
       
    64     else{
       
    65         QList<QVariant> paramList = params.value< QList<QVariant> >(); 
       
    66         QVariant indexVariant = paramList.at(0); 
       
    67         QModelIndex index = indexVariant.value<QModelIndex>();
       
    68         QVariant optionVariant = paramList.at(1); 
       
    69         mDisconOpt = (DisconnectOption)optionVariant.toInt();
       
    70         int error = KErrNone;
       
    71         
       
    72         mActiveHandling = true;
       
    73         mDeviceName = getDeviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
       
    74         mMajorRole = (index.data(BtDeviceModel::MajorPropertyRole)).toInt();
       
    75         
       
    76         QString strBtAddr = getDeviceModel()->data(index,BtDeviceModel::ReadableBdaddrRole).toString();
       
    77         
       
    78         // todo: address converting should be simplified. check other delegates for example.
       
    79         
       
    80         TPtrC ptrName(reinterpret_cast<const TText*>(strBtAddr.constData()));
       
    81             
       
    82         RBuf16 btName;
       
    83         error = btName.Create(ptrName.Length());
       
    84         
       
    85         if(error == KErrNone) {
       
    86             btName.Copy(ptrName);
       
    87             mBtEngAddr.SetReadable(btName);
       
    88             if (mDisconOpt == ServiceLevel){
       
    89                 disconnectSeviceLevel();
       
    90             }
       
    91             else if (mDisconOpt == PhysicalLink){
       
    92                 disconnectPhysicalLink();       
       
    93             }
       
    94         }
       
    95         btName.Close();
       
    96         
       
    97         if(error) {
       
    98             emit commandCompleted(error);
       
    99         }
       
   100     }  
       
   101 }
       
   102 
       
   103 
       
   104 void BtDelegateDisconnect::disconnectAllConnections_service(){
       
   105     
       
   106         TBuf<KBTDevAddrSize*3> addrBuf;
       
   107         mDevAddrArray[mAddrArrayIndex].GetReadable(addrBuf);
       
   108         QString btStringAddr= QString::fromUtf16( addrBuf.Ptr(), addrBuf.Length());
       
   109         QModelIndex start = getDeviceModel()->index(0,0);
       
   110         QModelIndexList indexList = getDeviceModel()->match(start,BtDeviceModel::ReadableBdaddrRole, btStringAddr);
       
   111         QModelIndex index = indexList.at(0);
       
   112         
       
   113         mDeviceName = getDeviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
       
   114         mBtEngAddr = mDevAddrArray[mAddrArrayIndex];
       
   115         mMajorRole = (index.data(BtDeviceModel::MajorPropertyRole)).toInt();
       
   116         
       
   117         disconnectSeviceLevel();        
       
   118 }
       
   119 
       
   120 void BtDelegateDisconnect::disconnectAllConnections_physical(){
       
   121     
       
   122         TBuf<KBTDevAddrSize*3> addrBuf;
       
   123         mDevAddrArray[mAddrArrayIndex].GetReadable(addrBuf);
       
   124         QString btStringAddr= QString::fromUtf16( addrBuf.Ptr(), addrBuf.Length());
       
   125         QModelIndex start = getDeviceModel()->index(0,0);
       
   126         QModelIndexList indexList = getDeviceModel()->match(start,BtDeviceModel::ReadableBdaddrRole, btStringAddr);
       
   127         QModelIndex index = indexList.at(0);
       
   128         
       
   129         mDeviceName = getDeviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
       
   130         mBtEngAddr = mDevAddrArray[mAddrArrayIndex];
       
   131         
       
   132         disconnectPhysicalLink();
       
   133         
       
   134 }
       
   135 void BtDelegateDisconnect::disconnectSeviceLevel(){
       
   136     int err;
       
   137     TBTEngConnectionStatus connStatus = EBTEngNotConnected;
       
   138     err = mBtengConnMan->IsConnected(mBtEngAddr, connStatus);
       
   139     if (connStatus == EBTEngConnected){
       
   140         err = mBtengConnMan->Disconnect(mBtEngAddr, EBTDiscGraceful);
       
   141     }
       
   142     if(err) {
       
   143         if (mDisconOpt == AllOngoingConnections){
       
   144             disconnectServiceLevelCompleted(err);
       
   145         }
       
   146         else{ 
       
   147             emit commandCompleted(err);
       
   148         }
       
   149     }
       
   150 }
       
   151         
       
   152 void BtDelegateDisconnect::disconnectPhysicalLink(){
       
   153     int err;
       
   154     if ( !mSocketServ.Handle() ) {
       
   155         err = mSocketServ.Connect();
       
   156     }
       
   157     if ( !err && !mPhyLinks ) {
       
   158         TRAP( err, 
       
   159             mPhyLinks = CBluetoothPhysicalLinks::NewL( *this, mSocketServ ) );
       
   160         Q_CHECK_PTR( mPhyLinks );
       
   161     }
       
   162     err = mPhyLinks->Disconnect( mBtEngAddr );
       
   163     if(err) {
       
   164         if (mDisconOpt == AllOngoingConnections){
       
   165             disconnectPhysicalLinkCompleted(err);
       
   166         }
       
   167         else{ 
       
   168             emit commandCompleted(err);
       
   169         }
       
   170     }
       
   171     
       
   172 }
       
   173 
       
   174 void BtDelegateDisconnect::disconnectServiceLevelCompleted(int err){
       
   175     if (mDisconOpt == ServiceLevel){
       
   176         mActiveHandling = false;
       
   177         emit commandCompleted(err);
       
   178     }
       
   179     else if (mDisconOpt == AllOngoingConnections){
       
   180         if (err){
       
   181             mActiveHandling = false;
       
   182             emit commandCompleted(err);
       
   183         }
       
   184         else{
       
   185             mAddrArrayIndex++;
       
   186             if ( mAddrArrayIndex < mDevAddrArray.Count()){
       
   187                 disconnectAllConnections_service();
       
   188             }
       
   189             else{
       
   190                 mDevAddrArray.Reset();
       
   191                 err = mBtengConnMan->GetConnectedAddresses(mDevAddrArray);
       
   192                 if(err) {
       
   193                     emit commandCompleted(err);
       
   194                     return;
       
   195                 }
       
   196                 mAddrArrayIndex = 0;
       
   197                 //connect( mDisconnectDelegate, SIGNAL(commandCompleted(int)), this, SLOT(disconnectPhysicalLinkCompleted(int)) );         
       
   198                 disconnectAllConnections_physical();
       
   199             }
       
   200         }
       
   201     }
       
   202 }
       
   203 
       
   204 void BtDelegateDisconnect::disconnectPhysicalLinkCompleted(int err){
       
   205     if (mDisconOpt == PhysicalLink){
       
   206         //emitCommandComplete(err);
       
   207         mActiveHandling = false;
       
   208         emit commandCompleted(err);
       
   209     }
       
   210     else if (mDisconOpt == AllOngoingConnections){
       
   211         if (err){
       
   212             mActiveHandling = false;
       
   213             emit commandCompleted(err);
       
   214         }
       
   215         else{
       
   216             mAddrArrayIndex++;
       
   217             if ( mAddrArrayIndex < mDevAddrArray.Count()){
       
   218                 disconnectAllConnections_physical();
       
   219             }
       
   220             else{
       
   221                 //TODO: check if there is still ongoing connection from BTEngVonnMan. and close them again if there is any new 
       
   222                 mActiveHandling = false;
       
   223                 emit commandCompleted(err);
       
   224             }
       
   225         }
       
   226         
       
   227     }
       
   228     
       
   229 }
       
   230 void BtDelegateDisconnect::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
       
   231                                    RBTDevAddrArray* aConflicts )
       
   232 {
       
   233     Q_UNUSED(aAddr);
       
   234     Q_UNUSED(aConflicts);  
       
   235     Q_UNUSED(aErr);
       
   236 }
       
   237 
       
   238 void BtDelegateDisconnect::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
       
   239 {
       
   240     if ( mBtEngAddr != aAddr || !mActiveHandling ) {  
       
   241         return;
       
   242     }
       
   243     DisplayCommandCompleteNotif(aErr);
       
   244     disconnectServiceLevelCompleted(aErr);    
       
   245 }
       
   246 
       
   247 
       
   248 void BtDelegateDisconnect::cancel()
       
   249 {
       
   250     
       
   251 }
       
   252 
       
   253 void BtDelegateDisconnect::HandleCreateConnectionCompleteL( TInt err ){
       
   254     Q_UNUSED( err );
       
   255 }
       
   256 
       
   257 void BtDelegateDisconnect::HandleDisconnectCompleteL( TInt err ){
       
   258     if ( !mActiveHandling ) {  
       
   259         return;
       
   260     } 
       
   261     disconnectPhysicalLinkCompleted(err);
       
   262           
       
   263 }
       
   264 
       
   265 void BtDelegateDisconnect::HandleDisconnectAllCompleteL( TInt err ){
       
   266     Q_UNUSED( err );
       
   267 }
       
   268 
       
   269 void BtDelegateDisconnect::DisplayCommandCompleteNotif(int error)
       
   270 {
       
   271     
       
   272     if(error == KErrNone) {
       
   273         // success, show indicator with connection status
       
   274         HbIcon icon = getBadgedDeviceTypeIcon( mCod, mMajorRole, 0 );  // no badging required, only icon
       
   275         QString str( hbTrId("txt_bt_dpopinfo_disconnected_from_1") );
       
   276         HbNotificationDialog::launchDialog( icon, hbTrId("txt_bt_dpophead_disconnected"), 
       
   277             str.arg(mDeviceName) );  
       
   278     }
       
   279 	
       
   280 }
       
   281 
       
   282