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