bluetoothengine/btui/btuidelegate/btdelegatepower.cpp
changeset 40 997690c3397a
parent 33 837dcc42fd6a
child 41 0b2439c3e397
equal deleted inserted replaced
37:91746b151f97 40:997690c3397a
    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     }
       
    71     
       
    72     // perform power on/off operation
       
    73     if ( mReqPowerState == EBTPowerOff ){ 
    52         switchBTOff();     
    74         switchBTOff();     
    53     }
    75     }
    54     else{//turn power ON  
    76     else if ( mReqPowerState == EBTPowerOn ) {
    55     
       
    56         switchBTOn();
    77         switchBTOn();
    57     }
    78     }
    58 }
    79 }
    59        
    80        
    60     
    81     
    62 void BtDelegatePower::switchBTOn()
    83 void BtDelegatePower::switchBTOn()
    63 {
    84 {
    64     int err = 0;
    85     int err = 0;
    65     
    86     
    66     //check if device is in OFFLINE mode first
    87     //check if device is in OFFLINE mode first
    67     TBTEnabledInOfflineMode enabledInOffline = EBTDisabledInOfflineMode;
    88     bool btEnabledInOffline = false;
    68     if (checkOfflineMode(enabledInOffline)){
    89     if (checkOfflineMode(btEnabledInOffline)){  // offline mode is active
    69     //if (1){
    90         if (btEnabledInOffline){
    70         if (enabledInOffline){
       
    71         //if (1){
       
    72             // BT is allowed to be enabled in offline mode, show query.
    91             // BT is allowed to be enabled in offline mode, show query.
    73             HbMessageBox::question( tr("Turn Bluetooth on in offline mode?"),this, 
    92             HbMessageBox::question( hbTrId("txt_bt_info_trun_bluetooth_on_ini_offline_mode" ),this, 
    74 				SLOT(btOnQuestionClose(HbAction*)));
    93 				SLOT(btOnQuestionClose(HbAction*)));
    75 
    94 
    76         }
    95         }
    77         else{
    96         else{
    78             //if BT is not allowed to be enabled in offline mode, show message and complete
    97             //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, 
    98             HbMessageBox::warning( hbTrId("txt_bt_info_bluetooth_not_allowed_to_be_turned_on" ),this, 
    80 				SLOT(btOnWarningClose()));
    99 				SLOT(btOnWarningClose()));
    81         }
   100         }
    82         
   101         
    83     }
   102     }
    84     else{
   103     else { // offline mode is not active
    85         //set BT on if the not in offline mode
   104         mActiveHandling = true;
    86         err = mBtengSettings->SetPowerState((TBTPowerStateValue)(1));
   105         err = mBtengSettings->SetPowerState(EBTPowerOn);
    87     }
   106     }
    88     
   107     
    89     if ( err ) {
   108     if ( err ) {
    90         QString info = "Unable to switch BT power ON" ;
   109         //TODO: handle the error here
    91         emit commandCompleted(KErrGeneral);
   110         emit commandCompleted(KErrGeneral);
    92     }
   111     }
    93     
   112     
    94 }
   113 }
    95 
   114 
    98     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
   117     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
    99     int err = 0;
   118     int err = 0;
   100     if(action == dlg->actions().at(0)) 
   119     if(action == dlg->actions().at(0)) 
   101     {
   120     {
   102         //user chooses "yes" for using BT in offline 
   121         //user chooses "yes" for using BT in offline 
   103         err = mBtengSettings->SetPowerState((TBTPowerStateValue)(1));
   122         mActiveHandling = true;
       
   123         err = mBtengSettings->SetPowerState(EBTPowerOn);
   104     }
   124     }
   105     else
   125     else
   106     {
   126     {
   107         //if user chooses "NO", emits the signal
   127         //if user chooses "NO", emits the signal
   108         emit commandCompleted(KErrNone);
   128         emit commandCompleted(KErrNone);
   109            
   129            
   110     }     
   130     }     
   111     if ( err ) {
   131     if ( err ) {
   112         QString info = "Unable to switch BT power ON" ;
   132         //TODO: handle the error here
   113         emit commandCompleted(KErrGeneral);
   133         emit commandCompleted(KErrGeneral);
   114     }
   134     }
   115 }
   135 }
   116 
   136 
   117 void BtDelegatePower::btOnWarningClose()
   137 void BtDelegatePower::btOnWarningClose()
   122 
   142 
   123 
   143 
   124 void BtDelegatePower::switchBTOff()
   144 void BtDelegatePower::switchBTOff()
   125 {
   145 {
   126     int err = 0;
   146     int err = 0;
   127     err = mBtengSettings->SetPowerState((TBTPowerStateValue)(0));
   147     
   128     
   148     CBTEngConnMan *btengConnMan = 0;
   129     if ( err ) {
   149     TRAP(err, btengConnMan = CBTEngConnMan::NewL(this));
   130         QString info = "Unable to switch BT power OFF" ;
   150     Q_CHECK_PTR( btengConnMan );
       
   151     RBTDevAddrArray devAddrArray;
       
   152     err = btengConnMan->GetConnectedAddresses(devAddrArray);
       
   153     int count = devAddrArray.Count();
       
   154     //int count =3;
       
   155     devAddrArray.Close();
       
   156     delete btengConnMan;
       
   157     if( !err &&  count> 0 ){
       
   158             HbMessageBox::question( hbTrId("Turn Bluetooth off? There is an active connection." ),this, 
       
   159                         SLOT(btOffDialogClose(HbAction*)));
       
   160             
       
   161         }
       
   162         else{
       
   163             mActiveHandling = true;
       
   164             err = mBtengSettings->SetPowerState(EBTPowerOff);
       
   165             
       
   166             if ( err ) {
       
   167                //TODO: handle the error here
       
   168                emit commandCompleted(KErrGeneral);
       
   169             }
       
   170         
       
   171         }    
       
   172 }
       
   173 
       
   174 void BtDelegatePower::btOffDialogClose(HbAction *action)
       
   175 {
       
   176     HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
       
   177     if(action == dlg->actions().at(0)) 
       
   178     {
       
   179         //user chooses "yes" for closing active connection before power off
       
   180         mActiveHandling = true;
       
   181         disconnectOngoingConnections();
       
   182     }
       
   183     else
       
   184     {
       
   185         //if user chooses "NO", emits the signal
       
   186         emit commandCompleted(KErrNone);
       
   187            
       
   188     }     
       
   189     
       
   190 }
       
   191 
       
   192 void BtDelegatePower::disconnectOngoingConnections(){
       
   193     if (! mDisconnectDelegate){
       
   194         mDisconnectDelegate = BtDelegateFactory::newDelegate(
       
   195                                                 BtDelegate::Disconnect, getSettingModel(), getDeviceModel()); 
       
   196         connect( mDisconnectDelegate, SIGNAL(commandCompleted(int)), this, SLOT(disconnectDelegateCompleted(int)) );
       
   197             
       
   198     
       
   199     DisconnectOption discoOpt = AllOngoingConnections;
       
   200     QVariant param;
       
   201     param.setValue((int)discoOpt);
       
   202     mDisconnectDelegate->exec(param);
       
   203     }
       
   204 }
       
   205 
       
   206 void BtDelegatePower::disconnectDelegateCompleted(int err)
       
   207 {
       
   208     Q_UNUSED( err );
       
   209     //TODO: handle the return error here
       
   210     
       
   211     int error = mBtengSettings->SetPowerState(EBTPowerOff);
       
   212     if ( error ) {
       
   213         //TODO: handle the error here
   131         emit commandCompleted(KErrGeneral);
   214         emit commandCompleted(KErrGeneral);
   132     }
   215     }
   133         
   216     
   134 }
   217     
   135 
   218 }
   136 void BtDelegatePower::btOffDialogClose(HbAction *action)
   219 
   137 {
   220 
   138     Q_UNUSED( action );
   221 void BtDelegatePower::PowerStateChanged( TBTPowerStateValue aPowerState )
   139     
   222 {
   140 }
   223     // It is possible that others change power: no handling for these cases.
   141 
   224     if ( !mActiveHandling ) {
   142 void BtDelegatePower::PowerStateChanged( TBTPowerStateValue aState )
   225         return;
   143 {
   226     } 
   144     Q_UNUSED( aState );
   227     mActiveHandling = false;
   145     emit commandCompleted(KErrNone);
   228     
       
   229     if ( mReqPowerState == aPowerState ) {
       
   230         // power state changed successfully
       
   231         emit commandCompleted( KErrNone );
       
   232     }
       
   233     else {
       
   234         // the actual power state is not the same as we requested,
       
   235         // command failed:
       
   236         // ToDo:  show error note?
       
   237         emit commandCompleted( KErrGeneral );
       
   238     }
   146 }
   239 }
   147 
   240 
   148 //Method derived from MBTEngSettingsObserver, no need to be implemented here
   241 //Method derived from MBTEngSettingsObserver, no need to be implemented here
   149 void BtDelegatePower::VisibilityModeChanged( TBTVisibilityMode aState )
   242 void BtDelegatePower::VisibilityModeChanged( TBTVisibilityMode aState )
   150 {
   243 {
   151     Q_UNUSED( aState );
   244     Q_UNUSED( aState );
   152 }
   245 }
   153 
   246 
   154 bool BtDelegatePower::checkOfflineMode(TBTEnabledInOfflineMode& aEnabledInOffline)
   247 void BtDelegatePower::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
   155 {
   248                                    RBTDevAddrArray* aConflicts )
   156     TCoreAppUIsNetworkConnectionAllowed offline = ECoreAppUIsNetworkConnectionAllowed;  
   249 {
       
   250     Q_UNUSED(aAddr);
       
   251     Q_UNUSED(aErr);
       
   252     Q_UNUSED(aConflicts);  
       
   253     /*
       
   254     if ( mBtEngAddr != aAddr ) {  // callback coming for some other device
       
   255         return;
       
   256     }
       
   257     emitCommandComplete(aErr);
       
   258     */
       
   259 }
       
   260 
       
   261 void BtDelegatePower::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr )
       
   262 {
       
   263     Q_UNUSED(aAddr);
       
   264     Q_UNUSED(aErr);    
       
   265 }
       
   266 
       
   267 /*!
       
   268    Returns true if offline mode is on, parameter returns true if BT is allowed 
       
   269    in offline mode
       
   270  */
       
   271 bool BtDelegatePower::checkOfflineMode(bool& btEnabledInOffline)
       
   272 {
       
   273     TCoreAppUIsNetworkConnectionAllowed offLineMode; 
       
   274     TBTEnabledInOfflineMode btEnabled;
   157    
   275    
   158     mBtengSettings->GetOfflineModeSettings(offline, aEnabledInOffline);
   276     mBtengSettings->GetOfflineModeSettings(offLineMode, btEnabled);
   159     return (!offline);
   277     
   160     
   278     btEnabledInOffline = (btEnabled == EBTEnabledInOfflineMode);
   161 }
   279     return (offLineMode == ECoreAppUIsNetworkConnectionNotAllowed);
   162 
   280 }
   163 /*if (params.toBool()) {  // turning power on
   281 
   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;