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