|
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 |
|
18 |
|
19 #include "btdelegatepower.h" |
|
20 #include "btqtconstants.h" |
|
21 #include <btabstractdelegate.h> |
|
22 #include <btdelegatefactory.h> |
|
23 #include <btsettingmodel.h> |
|
24 #include <btdevicemodel.h> |
|
25 #include <hbmessagebox.h> |
|
26 #include <bluetoothuitrace.h> |
|
27 #include <hbaction.h> |
|
28 |
|
29 /*! |
|
30 Constructor. |
|
31 */ |
|
32 BtDelegatePower::BtDelegatePower( |
|
33 BtSettingModel* settingModel, |
|
34 BtDeviceModel* deviceModel, QObject *parent ) |
|
35 : BtAbstractDelegate( settingModel, deviceModel, parent ), |
|
36 mDisconnectDelegate(0) |
|
37 { |
|
38 TRAP_IGNORE( mBtengSettings = CBTEngSettings::NewL(this) ); |
|
39 Q_CHECK_PTR( mBtengSettings ); |
|
40 mActiveHandling = false; |
|
41 } |
|
42 |
|
43 /*! |
|
44 Destructor. |
|
45 */ |
|
46 BtDelegatePower::~BtDelegatePower() |
|
47 { |
|
48 delete mDisconnectDelegate; |
|
49 delete mBtengSettings; |
|
50 } |
|
51 |
|
52 /*! |
|
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 ) |
|
57 { |
|
58 mReqPowerState = BtEngPowerState((PowerStateQtValue)powerState.toInt()); |
|
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 ){ |
|
75 switchBTOff(); |
|
76 } |
|
77 else if ( mReqPowerState == EBTPowerOn ) { |
|
78 switchBTOn(); |
|
79 } |
|
80 } |
|
81 |
|
82 |
|
83 |
|
84 void BtDelegatePower::switchBTOn() |
|
85 { |
|
86 int err = 0; |
|
87 |
|
88 //check if device is in OFFLINE mode first |
|
89 bool btEnabledInOffline = false; |
|
90 if (checkOfflineMode(btEnabledInOffline)){ // offline mode is active |
|
91 if (btEnabledInOffline){ |
|
92 // BT is allowed to be enabled in offline mode, show query. |
|
93 HbMessageBox::question( hbTrId("txt_bt_info_trun_bluetooth_on_ini_offline_mode" ),this, |
|
94 SLOT(btOnQuestionClose(int)), HbMessageBox::Yes | HbMessageBox::No ); |
|
95 |
|
96 } |
|
97 else{ |
|
98 //if BT is not allowed to be enabled in offline mode, show message and complete |
|
99 HbMessageBox::warning( hbTrId("txt_bt_info_bluetooth_not_allowed_to_be_turned_on" ),this, |
|
100 SLOT(btOnWarningClose())); |
|
101 } |
|
102 |
|
103 } |
|
104 else { // offline mode is not active |
|
105 mActiveHandling = true; |
|
106 err = mBtengSettings->SetPowerState(EBTPowerOn); |
|
107 } |
|
108 |
|
109 if ( err ) { |
|
110 //TODO: handle the error here |
|
111 emit commandCompleted(KErrGeneral); |
|
112 } |
|
113 |
|
114 } |
|
115 |
|
116 void BtDelegatePower::btOnQuestionClose(int action) |
|
117 { |
|
118 int err = 0; |
|
119 if(action == HbMessageBox::Yes) |
|
120 { |
|
121 //user chooses "yes" for using BT in offline |
|
122 mActiveHandling = true; |
|
123 err = mBtengSettings->SetPowerState(EBTPowerOn); |
|
124 } |
|
125 else |
|
126 { |
|
127 //if user chooses "NO", emits the signal |
|
128 emit commandCompleted(KErrNone); |
|
129 |
|
130 } |
|
131 if ( err ) { |
|
132 //TODO: handle the error here |
|
133 emit commandCompleted(KErrGeneral); |
|
134 } |
|
135 } |
|
136 |
|
137 void BtDelegatePower::btOnWarningClose() |
|
138 { |
|
139 emit commandCompleted(KErrNone); |
|
140 } |
|
141 |
|
142 |
|
143 |
|
144 void BtDelegatePower::switchBTOff() |
|
145 { |
|
146 int err = 0; |
|
147 |
|
148 CBTEngConnMan *btengConnMan = 0; |
|
149 TRAP(err, btengConnMan = CBTEngConnMan::NewL(this)); |
|
150 Q_CHECK_PTR( btengConnMan ); |
|
151 RBTDevAddrArray devAddrArray; |
|
152 err = btengConnMan->GetConnectedAddresses(devAddrArray); |
|
153 if ( err != KErrNone) { |
|
154 //TODO: handle the error here |
|
155 emit commandCompleted(err); |
|
156 return; |
|
157 } |
|
158 int count = devAddrArray.Count(); |
|
159 devAddrArray.Close(); |
|
160 delete btengConnMan; |
|
161 if( count> 0 ){ |
|
162 mActiveHandling = true; |
|
163 disconnectOngoingConnections(); |
|
164 } |
|
165 else{ |
|
166 mActiveHandling = true; |
|
167 err = mBtengSettings->SetPowerState(EBTPowerOff); |
|
168 |
|
169 if ( err ) { |
|
170 //TODO: handle the error here |
|
171 emit commandCompleted(KErrGeneral); |
|
172 } |
|
173 |
|
174 } |
|
175 } |
|
176 /* |
|
177 void BtDelegatePower::btOffDialogClose(HbAction *action) |
|
178 { |
|
179 HbMessageBox *dlg = static_cast<HbMessageBox*>(sender()); |
|
180 if(action == dlg->actions().at(0)) |
|
181 { |
|
182 //user chooses "yes" for closing active connection before power off |
|
183 mActiveHandling = true; |
|
184 disconnectOngoingConnections(); |
|
185 } |
|
186 else |
|
187 { |
|
188 //if user chooses "NO", emits the signal |
|
189 emit commandCompleted(KErrNone); |
|
190 |
|
191 } |
|
192 |
|
193 } |
|
194 */ |
|
195 void BtDelegatePower::disconnectOngoingConnections(){ |
|
196 if (! mDisconnectDelegate){ |
|
197 mDisconnectDelegate = BtDelegateFactory::newDelegate( |
|
198 BtDelegate::Disconnect, getSettingModel(), getDeviceModel()); |
|
199 connect( mDisconnectDelegate, SIGNAL(commandCompleted(int)), this, SLOT(disconnectDelegateCompleted(int)) ); |
|
200 |
|
201 |
|
202 DisconnectOption discoOpt = AllOngoingConnections; |
|
203 QVariant param; |
|
204 param.setValue((int)discoOpt); |
|
205 mDisconnectDelegate->exec(param); |
|
206 } |
|
207 } |
|
208 |
|
209 void BtDelegatePower::disconnectDelegateCompleted(int err) |
|
210 { |
|
211 Q_UNUSED( err ); |
|
212 //TODO: handle the return error here |
|
213 |
|
214 int error = mBtengSettings->SetPowerState(EBTPowerOff); |
|
215 if ( error ) { |
|
216 //TODO: handle the error here |
|
217 emit commandCompleted(KErrGeneral); |
|
218 } |
|
219 |
|
220 |
|
221 } |
|
222 |
|
223 |
|
224 void BtDelegatePower::PowerStateChanged( TBTPowerStateValue aPowerState ) |
|
225 { |
|
226 // It is possible that others change power: no handling for these cases. |
|
227 if ( !mActiveHandling ) { |
|
228 return; |
|
229 } |
|
230 mActiveHandling = false; |
|
231 |
|
232 if ( mReqPowerState == aPowerState ) { |
|
233 // power state changed successfully |
|
234 emit commandCompleted( KErrNone ); |
|
235 } |
|
236 else { |
|
237 // the actual power state is not the same as we requested, |
|
238 // command failed: |
|
239 // ToDo: show error note? |
|
240 emit commandCompleted( KErrGeneral ); |
|
241 } |
|
242 } |
|
243 |
|
244 //Method derived from MBTEngSettingsObserver, no need to be implemented here |
|
245 void BtDelegatePower::VisibilityModeChanged( TBTVisibilityMode aState ) |
|
246 { |
|
247 Q_UNUSED( aState ); |
|
248 } |
|
249 |
|
250 void BtDelegatePower::ConnectComplete( TBTDevAddr& aAddr, TInt aErr, |
|
251 RBTDevAddrArray* aConflicts ) |
|
252 { |
|
253 Q_UNUSED(aAddr); |
|
254 Q_UNUSED(aErr); |
|
255 Q_UNUSED(aConflicts); |
|
256 /* |
|
257 if ( mBtEngAddr != aAddr ) { // callback coming for some other device |
|
258 return; |
|
259 } |
|
260 emitCommandComplete(aErr); |
|
261 */ |
|
262 } |
|
263 |
|
264 void BtDelegatePower::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr ) |
|
265 { |
|
266 Q_UNUSED(aAddr); |
|
267 Q_UNUSED(aErr); |
|
268 } |
|
269 |
|
270 /*! |
|
271 Returns true if offline mode is on, parameter returns true if BT is allowed |
|
272 in offline mode |
|
273 */ |
|
274 bool BtDelegatePower::checkOfflineMode(bool& btEnabledInOffline) |
|
275 { |
|
276 TCoreAppUIsNetworkConnectionAllowed offLineMode; |
|
277 TBTEnabledInOfflineMode btEnabled; |
|
278 |
|
279 mBtengSettings->GetOfflineModeSettings(offLineMode, btEnabled); |
|
280 |
|
281 btEnabledInOffline = (btEnabled == EBTEnabledInOfflineMode); |
|
282 return (offLineMode == ECoreAppUIsNetworkConnectionNotAllowed); |
|
283 } |
|
284 |