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 "btlocalsetting.h" |
|
20 #include <btdevice.h> |
|
21 #include <btmanclient.h> |
|
22 #include <bt_subscribe.h> |
|
23 #include "btqtconstants.h" |
|
24 |
|
25 const int KLocalDeviceNameWatcher = 10; |
|
26 const int KBtLinkCountWatcher = 11; |
|
27 |
|
28 /*! |
|
29 Constructor. |
|
30 */ |
|
31 BtLocalSetting::BtLocalSetting( BtSettingModel& model, QObject *parent ) |
|
32 : QObject( parent), mModel( model ), mLocalDeviceWatcher(0) |
|
33 { |
|
34 int err( 0 ); |
|
35 if (!err ) { |
|
36 err = mLocalDeviceKey.Attach( KPropertyUidBluetoothCategory, |
|
37 KPropertyKeyBluetoothGetRegistryTableChange ); |
|
38 } |
|
39 |
|
40 Q_CHECK_PTR( !err ); // other proper alternative? |
|
41 |
|
42 TRAP_IGNORE({ |
|
43 mBtengSetting = CBTEngSettings::NewL( this ); |
|
44 mLocalDeviceWatcher = CBtSimpleActive::NewL(*this, KLocalDeviceNameWatcher ); |
|
45 }); |
|
46 |
|
47 Q_CHECK_PTR( mBtengSetting ); |
|
48 Q_CHECK_PTR( mLocalDeviceWatcher ); |
|
49 |
|
50 for ( int i = 0; i < BtSettingModel::LocalSettingRowCount; ++i ) { |
|
51 // Initialize the list with empty values. |
|
52 mData.append( BtuiModelDataItem() ); |
|
53 } |
|
54 |
|
55 // subscribe to local device table change: |
|
56 mLocalDeviceKey.Subscribe( mLocalDeviceWatcher->RequestStatus() ); |
|
57 mLocalDeviceWatcher->GoActive(); |
|
58 |
|
59 // Get the device name |
|
60 TBTDeviceName deviceName; |
|
61 (void) mBtengSetting->GetLocalName( deviceName ); |
|
62 updateDeviceName( QString::fromUtf16( deviceName.Ptr(), deviceName.Length() ) ); |
|
63 |
|
64 // Get the power setting. |
|
65 TBTPowerStateValue power( EBTPowerOff ); |
|
66 (void) mBtengSetting->GetPowerState( power ); |
|
67 setPowerSetting( power ); |
|
68 |
|
69 // Get the visibility mode |
|
70 TBTVisibilityMode visibilityMode( EBTVisibilityModeNoScans ); |
|
71 (void) mBtengSetting->GetVisibilityMode( visibilityMode ); |
|
72 setVisibilityMode( visibilityMode ); |
|
73 } |
|
74 |
|
75 /*! |
|
76 Destructor. |
|
77 */ |
|
78 BtLocalSetting::~BtLocalSetting() |
|
79 { |
|
80 // delete main data structure |
|
81 delete mBtengSetting; |
|
82 delete mLocalDeviceWatcher; |
|
83 mLocalDeviceKey.Close(); |
|
84 |
|
85 // delete mBtLinkCountWatcher; |
|
86 //mBtLinkCountKey.Close(); |
|
87 } |
|
88 |
|
89 |
|
90 /*! |
|
91 Tells whether the given column is in the range of the setting list. |
|
92 |
|
93 \param row the row number to be checked |
|
94 \param col the column number to be checked |
|
95 |
|
96 \return true if the given row and column are valid; false otherwise. |
|
97 */ |
|
98 bool BtLocalSetting::isValid( int row, int column) const |
|
99 { |
|
100 return row >= 0 && row < mData.count() && column == 0; |
|
101 } |
|
102 |
|
103 /*! |
|
104 \return the total amount of rows. |
|
105 |
|
106 */ |
|
107 int BtLocalSetting::rowCount() const |
|
108 { |
|
109 return mData.count(); |
|
110 } |
|
111 |
|
112 /*! |
|
113 \return the total amount of columns. |
|
114 |
|
115 */ |
|
116 int BtLocalSetting::columnCount() const |
|
117 { |
|
118 return 1; |
|
119 } |
|
120 |
|
121 /*! |
|
122 Gets the value within a data item. |
|
123 \param val contains the value at return. |
|
124 \param row the row number which the value is from |
|
125 \param col the column number which the value is from |
|
126 \param role the role idenfier of the value. |
|
127 */ |
|
128 void BtLocalSetting::data(QVariant& val, int row, int col, int role ) const |
|
129 { |
|
130 if ( isValid( row, col ) ) { |
|
131 val = mData.at( row ).value( role ); |
|
132 } |
|
133 else { |
|
134 val = QVariant( QVariant::Invalid ); |
|
135 } |
|
136 } |
|
137 |
|
138 /*! |
|
139 Gets the whole item data at the specified column |
|
140 \param row the row number of the item data to be returned |
|
141 \param col the column number of the item data to be returned |
|
142 \return the item data |
|
143 */ |
|
144 BtuiModelDataItem BtLocalSetting::itemData( int row, int col ) const |
|
145 { |
|
146 if ( isValid( row, col ) ) { |
|
147 return mData.at( row ); |
|
148 } |
|
149 return BtuiModelDataItem(); |
|
150 } |
|
151 |
|
152 /*! |
|
153 Provides notification of changes in the power state |
|
154 of the Bluetooth hardware. |
|
155 |
|
156 \param state EBTPowerOff if the BT hardware has been turned off, |
|
157 EBTPowerOn if it has been turned on. |
|
158 */ |
|
159 void BtLocalSetting::PowerStateChanged( TBTPowerStateValue state ) |
|
160 { |
|
161 setPowerSetting( state ); |
|
162 mModel.emitDataChanged( BtSettingModel::PowerStateRow, 0, this ); |
|
163 } |
|
164 |
|
165 /*! |
|
166 Provides notification of changes in the discoverability |
|
167 mode of the Bluetooth hardware. |
|
168 \param state EBTDiscModeHidden if the BT hardware is in hidden mode, |
|
169 EBTDiscModeGeneral if it is in visible mode. |
|
170 */ |
|
171 void BtLocalSetting::VisibilityModeChanged( TBTVisibilityMode state ) |
|
172 { |
|
173 setVisibilityMode( state ); |
|
174 mModel.emitDataChanged( BtSettingModel::VisibilityRow, 0, this ); |
|
175 } |
|
176 |
|
177 void BtLocalSetting::RequestCompletedL( CBtSimpleActive* active, TInt status ) { |
|
178 Q_UNUSED( active ); |
|
179 Q_UNUSED( status ); |
|
180 if ( active->RequestId() == KLocalDeviceNameWatcher ) { |
|
181 mLocalDeviceKey.Subscribe( mLocalDeviceWatcher->RequestStatus() ); |
|
182 mLocalDeviceWatcher->GoActive(); |
|
183 updateDeviceName( QString() ); |
|
184 } |
|
185 } |
|
186 |
|
187 void BtLocalSetting::CancelRequest( TInt requestId ) { |
|
188 if ( requestId == KLocalDeviceNameWatcher ) { |
|
189 mLocalDeviceKey.Cancel(); |
|
190 } |
|
191 else if ( requestId == KBtLinkCountWatcher ) { |
|
192 //mBtLinkCountKey.Cancel(); |
|
193 } |
|
194 } |
|
195 |
|
196 void BtLocalSetting::HandleError( CBtSimpleActive* active, TInt error ) { |
|
197 Q_UNUSED( active ); |
|
198 Q_UNUSED( error ); |
|
199 } |
|
200 |
|
201 /*! |
|
202 Update local Bluetooth device name in the data store. |
|
203 @param name the latest Bluetooth name. |
|
204 */ |
|
205 void BtLocalSetting::updateDeviceName( const QString &name ) |
|
206 { |
|
207 // To-do: the data structure initialization is not impled yet in the model |
|
208 BtuiModelDataItem& item = |
|
209 mData[ BtSettingModel::LocalBtNameRow ]; |
|
210 |
|
211 bool setByUser = !name.isEmpty(); |
|
212 |
|
213 // The additional parameter is the flag indicating whether the |
|
214 // Bluetooth name has been set by the user. |
|
215 // The flag is set to true if the name has been set. |
|
216 // item[ BtSettingModel::SettingValueParamRole ] = QVariant( setByUser ); |
|
217 |
|
218 QString resolvedName( name ); |
|
219 if ( resolvedName.isEmpty() ) { |
|
220 // We get the default name as suggestion for the user to set. |
|
221 getNameFromRegistry( resolvedName ); |
|
222 } |
|
223 item[ BtSettingModel::settingDisplayRole ] = QVariant( resolvedName ); |
|
224 item[ BtSettingModel::SettingValueRole ] = QVariant( resolvedName ); |
|
225 } |
|
226 |
|
227 /*! |
|
228 Updates all values related to the power setting. |
|
229 */ |
|
230 void BtLocalSetting::setPowerSetting( TBTPowerStateValue state ) |
|
231 { |
|
232 BtuiModelDataItem& item = |
|
233 mData[ BtSettingModel::PowerStateRow ]; |
|
234 |
|
235 item[ BtSettingModel::SettingValueRole ] = QVariant( QtPowerMode(state) ); |
|
236 } |
|
237 |
|
238 void BtLocalSetting::setVisibilityMode( TBTVisibilityMode state ) |
|
239 { |
|
240 BtuiModelDataItem& item = mData[ BtSettingModel::VisibilityRow ]; |
|
241 |
|
242 item [ BtSettingModel::SettingValueRole ] = QVariant( QtVisibilityMode(state) ); |
|
243 } |
|
244 |
|
245 /*! |
|
246 Get local Bluetooth device name from BTRegistry. |
|
247 */ |
|
248 void BtLocalSetting::getNameFromRegistry( QString &name ) |
|
249 { |
|
250 RBTRegServ btRegServ; // Session with BTMan |
|
251 RBTLocalDevice btReg; // Subsession with local device table |
|
252 TBTLocalDevice localDev;// Data structure holding local device information |
|
253 |
|
254 TInt err = btRegServ.Connect(); |
|
255 if ( !err ) { |
|
256 err = btReg.Open( btRegServ ); |
|
257 } |
|
258 if ( !err ) { |
|
259 // Read the BT local name from BT Registry. |
|
260 err = btReg.Get( localDev ); |
|
261 } |
|
262 if ( !err ) { |
|
263 name = QString::fromUtf8( (const char*) localDev.DeviceName().Ptr(), (int) localDev.DeviceName().Length() ); |
|
264 } |
|
265 btReg.Close(); |
|
266 btRegServ.Close(); |
|
267 } |
|