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 "btdelegateremotedevname.h"
|
|
19 |
#include <btsettingmodel.h>
|
|
20 |
#include <btdevicemodel.h>
|
|
21 |
#include <e32base.h>
|
|
22 |
#include <bluetoothuitrace.h>
|
|
23 |
#include <QRegExp>
|
|
24 |
#include "btuidevtypemap.h"
|
|
25 |
|
|
26 |
|
|
27 |
BtDelegateRemoteDevName::BtDelegateRemoteDevName(BtSettingModel* settingModel,
|
|
28 |
BtDeviceModel* deviceModel,QObject *parent )
|
47
|
29 |
:BtAbstractDelegate( settingModel, deviceModel, parent ), mRegistryActive(0), mRegistryOpened(false)
|
42
|
30 |
{
|
|
31 |
|
|
32 |
}
|
|
33 |
|
|
34 |
BtDelegateRemoteDevName::~BtDelegateRemoteDevName()
|
|
35 |
{
|
|
36 |
delete mRegistryActive;
|
|
37 |
mSymName.Close();
|
47
|
38 |
if (mRegistryOpened)
|
|
39 |
mBtRegistry.Close();//there is crash is close the btregistry without opening it first
|
42
|
40 |
mBtRegServ.Close();
|
|
41 |
}
|
|
42 |
|
|
43 |
/*!
|
57
|
44 |
Returns the supported editor types.
|
|
45 |
\return the sum of supported editor types
|
|
46 |
*/
|
|
47 |
int BtDelegateRemoteDevName::supportedEditorTypes() const
|
|
48 |
{
|
|
49 |
return BtDelegate::ChangeDeviceFriendlyName;
|
|
50 |
}
|
|
51 |
|
|
52 |
/*!
|
42
|
53 |
Validate the bluetooth device name given by the user:
|
|
54 |
Extra spaces (' ', '\n', '\t' and '\r') from the beginning,
|
|
55 |
middle and the end of the name are always removed;
|
|
56 |
the maximum lengthof a name is 30.
|
|
57 |
|
|
58 |
TODO:Add duplicate name checking. If the new name duplicates
|
|
59 |
existing name in registry, prompt user to change the name
|
|
60 |
*/
|
|
61 |
|
|
62 |
bool BtDelegateRemoteDevName::validateName(QString &name )
|
|
63 |
{
|
|
64 |
// remove spaces at the beginning and end:
|
|
65 |
name = name.trimmed();
|
|
66 |
// regular expression of one or more consecutive spaces:
|
|
67 |
QRegExp rx("[ \n\t\r]+");
|
|
68 |
name.replace( rx, " ");
|
|
69 |
if (name.length() > 30 ) {
|
|
70 |
name.resize( 30 );
|
|
71 |
}
|
|
72 |
return name.length() > 0;
|
|
73 |
}
|
|
74 |
|
|
75 |
void BtDelegateRemoteDevName::exec( const QVariant ¶ms )
|
|
76 |
{
|
|
77 |
QList<QVariant> paramList = params.value< QList<QVariant> >();
|
|
78 |
QVariant indexVariant = paramList.at(0);
|
|
79 |
QModelIndex index = indexVariant.value<QModelIndex>();
|
|
80 |
QVariant nameVariant = paramList.at(1);
|
|
81 |
QString btRemoteDevName = nameVariant.toString();
|
|
82 |
|
47
|
83 |
if (!validateName(btRemoteDevName)){
|
57
|
84 |
emit delegateCompleted(KErrBadName, this);
|
47
|
85 |
return;
|
|
86 |
}
|
|
87 |
mNewName = btRemoteDevName;
|
57
|
88 |
|
42
|
89 |
int error = KErrNone;
|
|
90 |
TPtrC ptrName(reinterpret_cast<const TText*>(btRemoteDevName.constData()));
|
|
91 |
|
|
92 |
error = mSymName.Create(ptrName.Length());
|
|
93 |
|
|
94 |
// todo (review comment): missing proper exception handling.
|
|
95 |
if(error == KErrNone) {
|
|
96 |
mSymName.Copy(ptrName);
|
|
97 |
}
|
|
98 |
else{
|
57
|
99 |
emit delegateCompleted(error,this);
|
42
|
100 |
return;
|
|
101 |
}
|
|
102 |
|
57
|
103 |
QString strBtAddr = deviceModel()->data(index,
|
42
|
104 |
BtDeviceModel::ReadableBdaddrRole).toString();
|
|
105 |
|
|
106 |
TBuf<KBTDevAddrSize * 2> buffer(strBtAddr.utf16());
|
|
107 |
mSymaddr.SetReadable( buffer );
|
|
108 |
|
|
109 |
error = mBtRegServ.Connect();
|
|
110 |
if ( error != KErrNone && error != KErrAlreadyExists) {
|
57
|
111 |
emit delegateCompleted(error,this);
|
42
|
112 |
return;
|
|
113 |
}
|
|
114 |
|
|
115 |
error = mBtRegistry.Open( mBtRegServ ) ;
|
|
116 |
if ( error != KErrNone && error != KErrAlreadyExists) {
|
57
|
117 |
emit delegateCompleted(error,this);
|
42
|
118 |
return;
|
|
119 |
}
|
47
|
120 |
mRegistryOpened = true;
|
|
121 |
|
42
|
122 |
if (!mRegistryActive){
|
|
123 |
RequestIdentifiers requestId = Unknown;
|
|
124 |
TRAP(error, mRegistryActive = CBtSimpleActive::NewL(
|
|
125 |
*this, requestId));
|
|
126 |
if(error!=KErrNone) {
|
57
|
127 |
emit delegateCompleted(KErrGeneral, this);
|
42
|
128 |
return;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
//first check if this device is already in the registry
|
|
132 |
|
57
|
133 |
int majorRole = (deviceModel()->data(index,BtDeviceModel::MajorPropertyRole)).toInt();
|
|
134 |
int cod = (deviceModel()->data(index,BtDeviceModel::CoDRole)).toInt();
|
42
|
135 |
if (!(majorRole & BtuiDevProperty::InRegistry)) {
|
|
136 |
CBTDevice *symBtDevice;
|
|
137 |
TRAP( error, {
|
|
138 |
symBtDevice = CBTDevice::NewL( mSymaddr );
|
|
139 |
symBtDevice->SetDeviceClass(cod);
|
|
140 |
RequestIdentifiers requestId = AddDevice;
|
|
141 |
mRegistryActive->SetRequestId(requestId);
|
|
142 |
mBtRegistry.AddDeviceL(*symBtDevice, mRegistryActive->iStatus);
|
|
143 |
mRegistryActive->GoActive();
|
|
144 |
});
|
|
145 |
|
|
146 |
}
|
|
147 |
else{
|
|
148 |
RequestIdentifiers requestId = ModifyFriendlyName;
|
|
149 |
mRegistryActive->SetRequestId(requestId);
|
|
150 |
TRAP( error, {
|
|
151 |
mBtRegistry.ModifyFriendlyDeviceNameL(mSymaddr, mSymName, mRegistryActive->iStatus);
|
|
152 |
mRegistryActive->GoActive();
|
|
153 |
});
|
|
154 |
}
|
|
155 |
if ( error != KErrNone ) {
|
57
|
156 |
emit delegateCompleted(error,this);
|
42
|
157 |
}
|
|
158 |
|
|
159 |
}
|
|
160 |
|
|
161 |
void BtDelegateRemoteDevName::RequestCompletedL( CBtSimpleActive* aActive, TInt aStatus ){
|
|
162 |
|
|
163 |
if(aStatus != KErrNone){
|
57
|
164 |
emit delegateCompleted(aStatus, this);
|
42
|
165 |
return;
|
|
166 |
}
|
|
167 |
int error = KErrNone;
|
|
168 |
if ( aActive->RequestId() == AddDevice ){
|
|
169 |
RequestIdentifiers requestId = ModifyFriendlyName;
|
|
170 |
mRegistryActive->SetRequestId(requestId);
|
|
171 |
TRAP( error, {
|
|
172 |
mBtRegistry.ModifyFriendlyDeviceNameL(mSymaddr, mSymName, mRegistryActive->iStatus);
|
|
173 |
mRegistryActive->GoActive();
|
|
174 |
});
|
|
175 |
if(error != KErrNone){
|
57
|
176 |
emit delegateCompleted(error, this);
|
42
|
177 |
}
|
|
178 |
}
|
|
179 |
else if ( aActive->RequestId() == ModifyFriendlyName ){
|
57
|
180 |
emit delegateCompleted(error, this);
|
42
|
181 |
}
|
|
182 |
|
|
183 |
}
|
|
184 |
|
|
185 |
void BtDelegateRemoteDevName::CancelRequest( TInt aRequestId ){
|
|
186 |
if ( aRequestId == 1 ){
|
|
187 |
mBtRegistry.CancelRequest(mRegistryActive->RequestStatus());
|
57
|
188 |
emit delegateCompleted(KErrCancel, this);
|
42
|
189 |
}
|
|
190 |
|
|
191 |
}
|
|
192 |
|
|
193 |
void BtDelegateRemoteDevName::HandleError( CBtSimpleActive* aActive, TInt aError ){
|
|
194 |
//TODO: handle the error here
|
|
195 |
Q_UNUSED( aActive );
|
|
196 |
Q_UNUSED( aError );
|
57
|
197 |
emit delegateCompleted(KErrGeneral,this);
|
42
|
198 |
}
|