bluetoothengine/btui/btuidelegate/btdelegatepower.cpp
changeset 42 b72428996822
parent 31 a0ea99b6fa53
child 51 625f43ae9362
equal deleted inserted replaced
32:19bd632b5100 42:b72428996822
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 #include "btdelegatepower.h"
    19 #include "btdelegatepower.h"
       
    20 #include "btqtconstants.h"
       
    21 #include <btabstractdelegate.h>
       
    22 #include <btdelegatefactory.h>
    20 #include <btsettingmodel.h>
    23 #include <btsettingmodel.h>
    21 #include <btdevicemodel.h>
    24 #include <btdevicemodel.h>
    22 #include <hbmessagebox.h>
    25 #include <hbmessagebox.h>
    23 #include <bluetoothuitrace.h>
    26 #include <bluetoothuitrace.h>
    24 #include <hbaction.h>
    27 #include <hbaction.h>
    27     Constructor.
    30     Constructor.
    28  */
    31  */
    29 BtDelegatePower::BtDelegatePower(            
    32 BtDelegatePower::BtDelegatePower(            
    30         BtSettingModel* settingModel, 
    33         BtSettingModel* settingModel, 
    31         BtDeviceModel* deviceModel, QObject *parent )
    34         BtDeviceModel* deviceModel, QObject *parent )
    32     : BtAbstractDelegate( settingModel, deviceModel, parent )
    35     : BtAbstractDelegate( settingModel, deviceModel, parent ),
       
    36       mDisconnectDelegate(0)
    33 {
    37 {
    34     TRAP_IGNORE( mBtengSettings = CBTEngSettings::NewL(this) );
    38     TRAP_IGNORE( mBtengSettings = CBTEngSettings::NewL(this) );
    35     Q_CHECK_PTR( mBtengSettings );
    39     Q_CHECK_PTR( mBtengSettings );
    36     
    40     mActiveHandling = false;
    37 }
    41 }
    38 
    42 
    39 /*!
    43 /*!
    40     Destructor.
    44     Destructor.
    41  */
    45  */
    42 BtDelegatePower::~BtDelegatePower()
    46 BtDelegatePower::~BtDelegatePower()
    43 {
    47 {
       
    48     delete mDisconnectDelegate;
    44     delete mBtengSettings;
    49     delete mBtengSettings;
    45     
    50 }
    46 }
    51 
    47 
    52 /*!
    48 void BtDelegatePower::exec( const QVariant &params )
    53     Turns BT power on/off
       
    54     param powerState is the desired power state and is of type PowerStateQtValue
       
    55  */
       
    56 void BtDelegatePower::exec( const QVariant &powerState )
    49 {   
    57 {   
    50     if (params.toInt()){//turn power OFF
    58     mReqPowerState = BtEngPowerState((PowerStateQtValue)powerState.toInt());
    51         
    59     BTUI_ASSERT_X( (mReqPowerState == EBTPowerOff) || (mReqPowerState == EBTPowerOn), 
       
    60             "BtDelegatePower::exec()", "wrong power state value" );
       
    61     
       
    62     // get current power status
       
    63     TBTPowerStateValue curPowerState(EBTPowerOff);
       
    64     mBtengSettings->GetPowerState( curPowerState );
       
    65     
       
    66     // verify requested power is not the same as current status
       
    67     if ( mReqPowerState == curPowerState ) {
       
    68         // no need to do anything
       
    69         emit commandCompleted( KErrNone );
       
    70         return;
       
    71     }
       
    72     
       
    73     // perform power on/off operation
       
    74     if ( mReqPowerState == EBTPowerOff ){ 
    52         switchBTOff();     
    75         switchBTOff();     
    53     }
    76     }
    54     else{//turn power ON  
    77     else if ( mReqPowerState == EBTPowerOn ) {
    55     
       
    56         switchBTOn();
    78         switchBTOn();
    57     }
    79     }
    58 }
    80 }
    59        
    81        
    60     
    82     
    62 void BtDelegatePower::switchBTOn()
    84 void BtDelegatePower::switchBTOn()
    63 {
    85 {
    64     int err = 0;
    86     int err = 0;
    65     
    87     
    66     //check if device is in OFFLINE mode first
    88     //check if device is in OFFLINE mode first
    67     TBTEnabledInOfflineMode enabledInOffline = EBTDisabledInOfflineMode;
    89     bool btEnabledInOffline = false;
    68     if (checkOfflineMode(enabledInOffline)){
    90     if (checkOfflineMode(btEnabledInOffline)){  // offline mode is active
    69     //if (1){
    91         if (btEnabledInOffline){
    70         if (enabledInOffline){
       
    71         //if (1){
       
    72             // BT is allowed to be enabled in offline mode, show query.
    92             // BT is allowed to be enabled in offline mode, show query.
    73             HbMessageBox::question( tr("Turn Bluetooth on in offline mode?"),this, 
    93             HbMessageBox::question( hbTrId("txt_bt_info_trun_bluetooth_on_ini_offline_mode" ),this, 
    74 				SLOT(btOnQuestionClose(HbAction*)));
    94 				SLOT(btOnQuestionClose(HbAction*)));
    75 
    95 
    76         }
    96         }
    77         else{
    97         else{
    78             //if BT is not allowed to be enabled in offline mode, show message and complete
    98             //if BT is not allowed to be enabled in offline mode, show message and complete
    79             HbMessageBox::warning(tr("Bluetooth not allowed to be turned on in offline mode"),this, 
    99             HbMessageBox::warning( hbTrId("txt_bt_info_bluetooth_not_allowed_to_be_turned_on" ),this, 
    80 				SLOT(btOnWarningClose()));
   100 				SLOT(btOnWarningClose()));
    81         }
   101         }
    82         
   102         
    83     }
   103     }
    84     else{
   104     else { // offline mode is not active
    85         //set BT on if the not in offline mode
   105         mActiveHandling = true;
    86         err = mBtengSettings->SetPowerState((TBTPowerStateValue)(1));
   106         err = mBtengSettings->SetPowerState(EBTPowerOn);
    87     }
   107     }
    88     
   108     
    89     if ( err ) {
   109     if ( err ) {
    90         QString info = "Unable to switch BT power ON" ;
   110         //TODO: handle the error here
    91         emit commandCompleted(KErrGeneral);
   111         emit commandCompleted(KErrGeneral);
    92     }
   112     }
    93     
   113     
    94 }
   114 }
    95 
   115 
    98     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
   118     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
    99     int err = 0;
   119     int err = 0;
   100     if(action == dlg->actions().at(0)) 
   120     if(action == dlg->actions().at(0)) 
   101     {
   121     {
   102         //user chooses "yes" for using BT in offline 
   122         //user chooses "yes" for using BT in offline 
   103         err = mBtengSettings->SetPowerState((TBTPowerStateValue)(1));
   123         mActiveHandling = true;
       
   124         err = mBtengSettings->SetPowerState(EBTPowerOn);
   104     }
   125     }
   105     else
   126     else
   106     {
   127     {
   107         //if user chooses "NO", emits the signal
   128         //if user chooses "NO", emits the signal
   108         emit commandCompleted(KErrNone);
   129         emit commandCompleted(KErrNone);
   109            
   130            
   110     }     
   131     }     
   111     if ( err ) {
   132     if ( err ) {
   112         QString info = "Unable to switch BT power ON" ;
   133         //TODO: handle the error here
   113         emit commandCompleted(KErrGeneral);
   134         emit commandCompleted(KErrGeneral);
   114     }
   135     }
   115 }
   136 }
   116 
   137 
   117 void BtDelegatePower::btOnWarningClose()
   138 void BtDelegatePower::btOnWarningClose()
   122 
   143 
   123 
   144 
   124 void BtDelegatePower::switchBTOff()
   145 void BtDelegatePower::switchBTOff()
   125 {
   146 {
   126     int err = 0;
   147     int err = 0;
   127     err = mBtengSettings->SetPowerState((TBTPowerStateValue)(0));
   148     
   128     
   149     CBTEngConnMan *btengConnMan = 0;
   129     if ( err ) {
   150     TRAP(err, btengConnMan = CBTEngConnMan::NewL(this));
   130         QString info = "Unable to switch BT power OFF" ;
   151     Q_CHECK_PTR( btengConnMan );
       
   152     RBTDevAddrArray devAddrArray;
       
   153     err = btengConnMan->GetConnectedAddresses(devAddrArray);
       
   154     if ( err != KErrNone) {
       
   155        //TODO: handle the error here
       
   156        emit commandCompleted(err);
       
   157        return;
       
   158     }
       
   159     int count = devAddrArray.Count();
       
   160     devAddrArray.Close();
       
   161     delete btengConnMan;
       
   162     if( count> 0 ){
       
   163         mActiveHandling = true;
       
   164         disconnectOngoingConnections(); 
       
   165     }
       
   166     else{
       
   167         mActiveHandling = true;
       
   168         err = mBtengSettings->SetPowerState(EBTPowerOff);
       
   169         
       
   170         if ( err ) {
       
   171            //TODO: handle the error here
       
   172            emit commandCompleted(KErrGeneral);
       
   173         }
       
   174         
       
   175     }    
       
   176 }
       
   177 /*
       
   178 void BtDelegatePower::btOffDialogClose(HbAction *action)
       
   179 {
       
   180     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
       
   181     if(action == dlg->actions().at(0)) 
       
   182     {
       
   183         //user chooses "yes" for closing active connection before power off
       
   184         mActiveHandling = true;
       
   185         disconnectOngoingConnections();
       
   186     }
       
   187     else
       
   188     {
       
   189         //if user chooses "NO", emits the signal
       
   190         emit commandCompleted(KErrNone);
       
   191            
       
   192     }     
       
   193     
       
   194 }
       
   195 */
       
   196 void BtDelegatePower::disconnectOngoingConnections(){
       
   197     if (! mDisconnectDelegate){
       
   198         mDisconnectDelegate = BtDelegateFactory::newDelegate(
       
   199                                                 BtDelegate::Disconnect, getSettingModel(), getDeviceModel()); 
       
   200         connect( mDisconnectDelegate, SIGNAL(commandCompleted(int)), this, SLOT(disconnectDelegateCompleted(int)) );
       
   201             
       
   202     
       
   203     DisconnectOption discoOpt = AllOngoingConnections;
       
   204     QVariant param;
       
   205     param.setValue((int)discoOpt);
       
   206     mDisconnectDelegate->exec(param);
       
   207     }
       
   208 }
       
   209 
       
   210 void BtDelegatePower::disconnectDelegateCompleted(int err)
       
   211 {
       
   212     Q_UNUSED( err );
       
   213     //TODO: handle the return error here
       
   214     
       
   215     int error = mBtengSettings->SetPowerState(EBTPowerOff);
       
   216     if ( error ) {
       
   217         //TODO: handle the error here
   131         emit commandCompleted(KErrGeneral);
   218         emit commandCompleted(KErrGeneral);
   132     }
   219     }
   133         
   220     
   134 }
   221     
   135 
   222 }
   136 void BtDelegatePower::btOffDialogClose(HbAction *action)
   223 
   137 {
   224 
   138     Q_UNUSED( action );
   225 void BtDelegatePower::PowerStateChanged( TBTPowerStateValue aPowerState )
   139     
   226 {
   140 }
   227     // It is possible that others change power: no handling for these cases.
   141 
   228     if ( !mActiveHandling ) {
   142 void BtDelegatePower::PowerStateChanged( TBTPowerStateValue aState )
   229         return;
   143 {
   230     } 
   144     Q_UNUSED( aState );
   231     mActiveHandling = false;
   145     emit commandCompleted(KErrNone);
   232     
       
   233     if ( mReqPowerState == aPowerState ) {
       
   234         // power state changed successfully
       
   235         emit commandCompleted( KErrNone );
       
   236     }
       
   237     else {
       
   238         // the actual power state is not the same as we requested,
       
   239         // command failed:
       
   240         // ToDo:  show error note?
       
   241         emit commandCompleted( KErrGeneral );
       
   242     }
   146 }
   243 }
   147 
   244 
   148 //Method derived from MBTEngSettingsObserver, no need to be implemented here
   245 //Method derived from MBTEngSettingsObserver, no need to be implemented here
   149 void BtDelegatePower::VisibilityModeChanged( TBTVisibilityMode aState )
   246 void BtDelegatePower::VisibilityModeChanged( TBTVisibilityMode aState )
   150 {
   247 {
   151     Q_UNUSED( aState );
   248     Q_UNUSED( aState );
   152 }
   249 }
   153 
   250 
   154 bool BtDelegatePower::checkOfflineMode(TBTEnabledInOfflineMode& aEnabledInOffline)
   251 void BtDelegatePower::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
   155 {
   252                                    RBTDevAddrArray* aConflicts )
   156     TCoreAppUIsNetworkConnectionAllowed offline = ECoreAppUIsNetworkConnectionAllowed;  
   253 {
       
   254     Q_UNUSED(aAddr);
       
   255     Q_UNUSED(aErr);
       
   256     Q_UNUSED(aConflicts);  
       
   257     /*
       
   258     if ( mBtEngAddr != aAddr ) {  // callback coming for some other device
       
   259         return;
       
   260     }
       
   261     emitCommandComplete(aErr);
       
   262     */
       
   263 }
       
   264 
       
   265 void BtDelegatePower::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
       
   266 {
       
   267     Q_UNUSED(aAddr);
       
   268     Q_UNUSED(aErr);    
       
   269 }
       
   270 
       
   271 /*!
       
   272    Returns true if offline mode is on, parameter returns true if BT is allowed 
       
   273    in offline mode
       
   274  */
       
   275 bool BtDelegatePower::checkOfflineMode(bool& btEnabledInOffline)
       
   276 {
       
   277     TCoreAppUIsNetworkConnectionAllowed offLineMode; 
       
   278     TBTEnabledInOfflineMode btEnabled;
   157    
   279    
   158     mBtengSettings->GetOfflineModeSettings(offline, aEnabledInOffline);
   280     mBtengSettings->GetOfflineModeSettings(offLineMode, btEnabled);
   159     return (!offline);
   281     
   160     
   282     btEnabledInOffline = (btEnabled == EBTEnabledInOfflineMode);
   161 }
   283     return (offLineMode == ECoreAppUIsNetworkConnectionNotAllowed);
   162 
   284 }
   163 /*if (params.toBool()) {  // turning power on
   285 
   164 
       
   165         // find out if local device is in offline mode
       
   166         QModelIndex idx = mModel->index( Btuim::OfflineMode, 0);
       
   167         QVariant var = mModel->data( idx, Qt::EditRole );
       
   168         bool offlineMode = var.toBool();
       
   169 
       
   170         // find out whether BT is allowed in offline mode
       
   171         var = mModel->data( idx, Btuim::SettingAdditionalParam );
       
   172         bool activationAllowed = var.toBool();
       
   173         
       
   174         if (offlineMode) {
       
   175             // in offline mode
       
   176             if (activationAllowed) {
       
   177                 HbMessageBox *messageBox = new HbMessageBox(); 
       
   178                 // BT is allowed to be enabled in offline mode, show query.
       
   179                 if (messageBox->question( tr("Activate Bluetooth in offline mode?") )) {
       
   180                     ret = mModel->setData(index, value, role);
       
   181                 }
       
   182                 delete messageBox;
       
   183             }
       
   184             else {
       
   185                 // BT is not allowed to be activated in offline mode, show note.
       
   186 
       
   187                 HbDialog *mShowOnlyPopup = new HbDialog();
       
   188                 mShowOnlyPopup->setAttribute(Qt::WA_DeleteOnClose);
       
   189                 mShowOnlyPopup->setModal(false);
       
   190                 mShowOnlyPopup->setBackgroundFaded(false);
       
   191                 mShowOnlyPopup->setDismissPolicy( HbPopup::NoDismiss  );
       
   192                 mShowOnlyPopup->setTimeout( 5000 );  // 5 sec
       
   193                 HbLabel *label = new HbLabel( tr("Bluetooth is not allowed in offline mode") );
       
   194                 label->setAlignment(Qt::AlignCenter);
       
   195                 QSizeF popupSize(350,100);
       
   196                 mShowOnlyPopup->setMinimumSize(popupSize);
       
   197                 mShowOnlyPopup->setContentWidget(label);                
       
   198                 mShowOnlyPopup->show();
       
   199             }
       
   200         } 
       
   201         else {
       
   202             // not in offline mode, forward the request to model. 
       
   203             ret =  mModel->setData(index, value, role);    
       
   204         }
       
   205     }
       
   206     else {   // turning power off
       
   207         // first check if existing connections
       
   208         QModelIndex idx = mModel->index(Btuim::BtConnections, 0);
       
   209         QVariant var = mModel->data(idx, Qt::EditRole);
       
   210         bool ok;
       
   211         TInt connNum = var.toInt( &ok );
       
   212         BTUI_ASSERT_X( ok, "BtUiSettingsDelegate::setData", "wrong qvariant type");
       
   213         if (connNum) {
       
   214             // there is at least 1 active connection, show query.
       
   215             HbMessageBox *messageBox = new HbMessageBox(); 
       
   216             if (messageBox->question( tr("Turn Bluetooth off even though connections exist?") )) {
       
   217                 ret = mModel->setData(index, value, role);
       
   218             }
       
   219             delete messageBox;
       
   220         } 
       
   221         else {  
       
   222             // no active connections exist, forward the request to model.
       
   223             ret =  mModel->setData(index, value, role);
       
   224         }
       
   225     }*/
       
   226     //emit commandCompleted(err);
       
   227         //return ret;
       
   228     //return false;