bluetoothengine/btui/btuidelegate/btdelegatedisconnect.cpp
changeset 42 b72428996822
parent 31 a0ea99b6fa53
child 57 5ebadcda06cb
equal deleted inserted replaced
32:19bd632b5100 42:b72428996822
    13 *
    13 *
    14 * Description: 
    14 * Description: 
    15 *
    15 *
    16 */
    16 */
    17 #include "btdelegatedisconnect.h"
    17 #include "btdelegatedisconnect.h"
       
    18 #include "btuiutil.h"
       
    19 #include "btuiiconutil.h"
    18 #include <QModelIndex>
    20 #include <QModelIndex>
       
    21 #include <hblabel.h>
    19 #include <btsettingmodel.h>
    22 #include <btsettingmodel.h>
    20 #include <btdevicemodel.h>
    23 #include <btdevicemodel.h>
    21 #include <hbnotificationdialog.h>
    24 #include <hbnotificationdialog.h>
    22 
    25 
    23 BtDelegateDisconnect::BtDelegateDisconnect(            
    26 BtDelegateDisconnect::BtDelegateDisconnect(            
    24         BtSettingModel* settingModel, 
    27         BtSettingModel* settingModel, 
    25         BtDeviceModel* deviceModel, QObject *parent) :
    28         BtDeviceModel* deviceModel, QObject *parent) :
    26     BtAbstractDelegate(settingModel, deviceModel, parent), mBtengConnMan(0)
    29     BtAbstractDelegate(settingModel, deviceModel, parent), mBtengConnMan(0), mPhyLinks(0),
       
    30 	 mMajorRole(0), mActiveHandling(false), mAddrArrayIndex(0), mDisconOpt(DisconUnknown)
    27 {
    31 {
    28     
    32     
    29 }
    33 }
    30 
    34 
    31 BtDelegateDisconnect::~BtDelegateDisconnect()
    35 BtDelegateDisconnect::~BtDelegateDisconnect()
    32 {
    36 {
    33     delete mBtengConnMan;
    37     delete mBtengConnMan;
       
    38     delete mPhyLinks;
       
    39     mSocketServ.Close();
    34 }
    40 }
    35 
    41 
    36 void BtDelegateDisconnect::exec( const QVariant &params )
    42 void BtDelegateDisconnect::exec( const QVariant &params )
    37 {
    43 {
    38     int error = KErrNone;
    44     int err;
    39     QModelIndex index = params.value<QModelIndex>();
       
    40     
       
    41     mdeviceName = getDeviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
       
    42     
       
    43     QString strBtAddr = getDeviceModel()->data(index,BtDeviceModel::ReadableBdaddrRole).toString();
       
    44     
       
    45     if ( ! mBtengConnMan ){
    45     if ( ! mBtengConnMan ){
    46         TRAP_IGNORE( mBtengConnMan = CBTEngConnMan::NewL(this) );
    46         TRAP( err, mBtengConnMan = CBTEngConnMan::NewL(this) );
    47     }
    47     }
    48     Q_CHECK_PTR( mBtengConnMan );
    48     if(err) {
    49     
    49         emit commandCompleted(err);
    50     TPtrC ptrName(reinterpret_cast<const TText*>(strBtAddr.constData()));
    50         return;
    51         
    51     }
    52     RBuf16 btName;
    52     if (params.canConvert<int>()){
    53     error = btName.Create(ptrName.Length());
    53         mDisconOpt = (DisconnectOption)params.toInt();
    54     
    54         mActiveHandling = true;
    55     if(error == KErrNone) {
    55         
    56         btName.Copy(ptrName);
    56         if (mDisconOpt == AllOngoingConnections){
    57         mBtEngddr.SetReadable(btName);
    57             err = mBtengConnMan->GetConnectedAddresses(mDevAddrArray);
    58         error = mBtengConnMan->Disconnect(mBtEngddr, EBTDiscGraceful);
    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         }
    59         btName.Close();
    95         btName.Close();
    60     }
    96         
    61     
    97         if(error) {
    62     
    98             emit commandCompleted(error);
    63     if(error) {
    99         }
    64         emitCommandComplete(error);
   100     }  
    65     }
   101 }
    66     
   102 
    67 }
   103 
    68 
   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 }
    69 void BtDelegateDisconnect::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
   230 void BtDelegateDisconnect::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
    70                                    RBTDevAddrArray* aConflicts )
   231                                    RBTDevAddrArray* aConflicts )
    71 {
   232 {
    72     Q_UNUSED(aAddr);
   233     Q_UNUSED(aAddr);
    73     Q_UNUSED(aConflicts);  
   234     Q_UNUSED(aConflicts);  
    74     Q_UNUSED(aErr);
   235     Q_UNUSED(aErr);
    75 }
   236 }
    76 
   237 
    77 void BtDelegateDisconnect::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
   238 void BtDelegateDisconnect::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
    78 {
   239 {
    79     Q_UNUSED(aAddr);
   240     if ( mBtEngAddr != aAddr || !mActiveHandling ) {  
    80     emitCommandComplete(aErr);    
   241         return;
    81 }
   242     }
    82 
   243     DisplayCommandCompleteNotif(aErr);
    83 void BtDelegateDisconnect::PairingComplete( TBTDevAddr& aAddr, TInt aErr )
   244     disconnectServiceLevelCompleted(aErr);    
    84 {
   245 }
    85     Q_UNUSED(aAddr);
   246 
    86     Q_UNUSED(aErr);
       
    87 }
       
    88 
   247 
    89 void BtDelegateDisconnect::cancel()
   248 void BtDelegateDisconnect::cancel()
    90 {
   249 {
    91     
   250     
    92 }
   251 }
    93 
   252 
    94 void BtDelegateDisconnect::emitCommandComplete(int error)
   253 void BtDelegateDisconnect::HandleCreateConnectionCompleteL( TInt err ){
    95 {
   254     Q_UNUSED( err );
    96     QString str(hbTrId("Disconnected to %1"));
   255 }
    97     QString err(hbTrId("Disconnecting with %1 Failed"));
   256 
    98     
   257 void BtDelegateDisconnect::HandleDisconnectCompleteL( TInt err ){
    99     if(error != KErrNone) {
   258     if ( !mActiveHandling ) {  
   100         HbNotificationDialog::launchDialog(err.arg(mdeviceName));
   259         return;
   101     }
   260     } 
   102     else {
   261     disconnectPhysicalLinkCompleted(err);
   103         HbNotificationDialog::launchDialog(str.arg(mdeviceName));
   262           
   104     }
   263 }
   105 
   264 
   106     emit commandCompleted(error);
   265 void BtDelegateDisconnect::HandleDisconnectAllCompleteL( TInt err ){
   107 }
   266     Q_UNUSED( err );
   108 
   267 }
   109 
   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