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