bluetoothengine/btui/btuimodel/btsettingmodel_p.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     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 "btsettingmodel_p.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 BtSettingModelPrivate::BtSettingModelPrivate( 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 BtSettingModelPrivate::~BtSettingModelPrivate()
       
    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 BtSettingModelPrivate::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 BtSettingModelPrivate::rowCount() const
       
   108 {
       
   109     return mData.count();
       
   110 }
       
   111 
       
   112 /*!
       
   113     \return the total amount of columns.
       
   114     
       
   115 */
       
   116 int BtSettingModelPrivate::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 BtSettingModelPrivate::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 BtSettingModelPrivate::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 BtSettingModelPrivate::PowerStateChanged( TBTPowerStateValue state ) 
       
   160 {
       
   161     setPowerSetting( state );
       
   162     emit settingDataChanged( BtSettingModel::PowerStateRow, 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 BtSettingModelPrivate::VisibilityModeChanged( TBTVisibilityMode state )
       
   172 {
       
   173     setVisibilityMode( state );
       
   174     emit settingDataChanged( BtSettingModel::VisibilityRow, this );
       
   175 }
       
   176 
       
   177 void BtSettingModelPrivate::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 BtSettingModelPrivate::CancelRequest( TInt requestId ) {
       
   188     if ( requestId == KLocalDeviceNameWatcher ) {
       
   189         mLocalDeviceKey.Cancel();
       
   190     }
       
   191     else if ( requestId == KBtLinkCountWatcher ) {
       
   192         //mBtLinkCountKey.Cancel();
       
   193     }
       
   194 }
       
   195 
       
   196 void BtSettingModelPrivate::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 BtSettingModelPrivate::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 BtSettingModelPrivate::setPowerSetting( TBTPowerStateValue state )
       
   231 {
       
   232     BtuiModelDataItem& item = 
       
   233             mData[ BtSettingModel::PowerStateRow ];
       
   234     
       
   235     item[ BtSettingModel::SettingValueRole ] = QVariant( QtPowerMode(state) );
       
   236 }
       
   237 
       
   238 void BtSettingModelPrivate::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 BtSettingModelPrivate::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 }