bluetoothengine/btui/btuimodel/btlocalsetting.cpp
changeset 19 43824b19ee35
child 31 a0ea99b6fa53
equal deleted inserted replaced
17:f05641c183ff 19:43824b19ee35
       
     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 "btlocalsetting.h"
       
    20 #include <btdevice.h>
       
    21 //#include <QStringList>
       
    22 #include <btmanclient.h>
       
    23 #include <bt_subscribe.h>
       
    24 //#include <centralrepository.h>
       
    25 //#include <coreapplicationuissdkcrkeys.h>
       
    26 #include "btqtconstants.h"
       
    27 
       
    28 const int KLocalDeviceNameWatcher = 10;
       
    29 const int KBtLinkCountWatcher = 11;
       
    30 
       
    31 /*!
       
    32     Constructor.
       
    33  */
       
    34 BtLocalSetting::BtLocalSetting( BtuiModel& model, QObject *parent )
       
    35     : QObject( parent ), mModel(model), mLocalDeviceWatcher(0)
       
    36     {
       
    37     int err( 0 );
       
    38     if (!err ) {
       
    39         err = mLocalDeviceKey.Attach( KPropertyUidBluetoothCategory, 
       
    40                     KPropertyKeyBluetoothGetRegistryTableChange );
       
    41     }
       
    42     
       
    43     Q_CHECK_PTR( !err ); // other proper alternative?
       
    44 
       
    45     TRAP_IGNORE({
       
    46         mBtengSetting = CBTEngSettings::NewL( this );
       
    47         mLocalDeviceWatcher = CBtSimpleActive::NewL(*this, KLocalDeviceNameWatcher );
       
    48     });
       
    49     
       
    50     Q_CHECK_PTR( mBtengSetting );
       
    51     Q_CHECK_PTR( mLocalDeviceWatcher );
       
    52 
       
    53     for ( int i = 0; i < BtuiModel::LocalSettingColCount; ++i ) {
       
    54         // Initialize the list with empty values.
       
    55         mData.append( BtuiModelDataItem() );
       
    56     }
       
    57     
       
    58     // subscribe to local device table change:
       
    59     mLocalDeviceKey.Subscribe( mLocalDeviceWatcher->RequestStatus() );
       
    60     mLocalDeviceWatcher->GoActive();
       
    61     
       
    62     // Get the device name
       
    63     TBTDeviceName  deviceName;
       
    64     (void) mBtengSetting->GetLocalName( deviceName );
       
    65     updateDeviceName( QString::fromUtf16( deviceName.Ptr(), deviceName.Length() ) );
       
    66     
       
    67     // Get the power setting.
       
    68     TBTPowerStateValue power( EBTPowerOff );
       
    69     (void) mBtengSetting->GetPowerState( power );
       
    70     setPowerSetting( power );
       
    71     
       
    72     // Get the visibility mode
       
    73     TBTVisibilityMode visibilityMode( EBTVisibilityModeNoScans );
       
    74     (void) mBtengSetting->GetVisibilityMode( visibilityMode );
       
    75     setVisibilityMode( visibilityMode );
       
    76 }
       
    77 
       
    78 /*!
       
    79     Destructor.
       
    80  */
       
    81 BtLocalSetting::~BtLocalSetting()
       
    82 {
       
    83     // delete main data structure
       
    84     delete mBtengSetting;
       
    85     delete mLocalDeviceWatcher;
       
    86     mLocalDeviceKey.Close();
       
    87     
       
    88     // delete mBtLinkCountWatcher;
       
    89     //mBtLinkCountKey.Close();
       
    90 }
       
    91 
       
    92 
       
    93 bool BtLocalSetting::isValid( int column) const
       
    94 {
       
    95     return column < mData.count();
       
    96 }
       
    97 
       
    98 int BtLocalSetting::itemCount() const
       
    99 {
       
   100     return mData.count();
       
   101 }
       
   102 
       
   103 void BtLocalSetting::data(QVariant& val, int col, int role ) const
       
   104 {
       
   105     if ( isValid( col ) ) {
       
   106         val = mData.at( col ).value( role );
       
   107     }
       
   108     else {
       
   109         val = QVariant( QVariant::Invalid );
       
   110     }
       
   111 }
       
   112 
       
   113 BtuiModelDataItem BtLocalSetting::itemData( int col ) const
       
   114 {
       
   115     if ( isValid( col ) ) {
       
   116         return mData.at( col );
       
   117     }
       
   118     return BtuiModelDataItem();
       
   119 }
       
   120 
       
   121 
       
   122 /*!
       
   123     Provides notification of changes in the power state
       
   124     of the Bluetooth hardware.
       
   125 
       
   126     @param state EBTPowerOff if the BT hardware has been turned off,
       
   127                  EBTPowerOn if it has been turned on.
       
   128  */
       
   129 void BtLocalSetting::PowerStateChanged( TBTPowerStateValue state ) 
       
   130 {
       
   131     setPowerSetting( state );
       
   132     emit settingDataChanged( BtuiModel::LocalSettingRow, BtuiModel::PowerStateCol, this );
       
   133 }
       
   134 
       
   135 /*!
       
   136     Provides notification of changes in the discoverability
       
   137     mode of the Bluetooth hardware.
       
   138     @param state EBTDiscModeHidden if the BT hardware is in hidden mode,
       
   139                   EBTDiscModeGeneral if it is in visible mode.
       
   140  */
       
   141 void BtLocalSetting::VisibilityModeChanged( TBTVisibilityMode state )
       
   142 {
       
   143     setVisibilityMode( state );
       
   144     emit settingDataChanged( BtuiModel::LocalSettingRow, BtuiModel::VisibilityCol, this );
       
   145 }
       
   146 
       
   147 void BtLocalSetting::RequestCompletedL( CBtSimpleActive* active, TInt status ) {
       
   148     Q_UNUSED( active );
       
   149     Q_UNUSED( status );
       
   150     if ( active->RequestId() == KLocalDeviceNameWatcher ) {
       
   151         mLocalDeviceKey.Subscribe( mLocalDeviceWatcher->RequestStatus() );
       
   152         mLocalDeviceWatcher->GoActive();
       
   153         updateDeviceName( QString() );
       
   154     }
       
   155 }
       
   156 
       
   157 void BtLocalSetting::CancelRequest( TInt requestId ) {
       
   158     if ( requestId == KLocalDeviceNameWatcher ) {
       
   159         mLocalDeviceKey.Cancel();
       
   160     }
       
   161     else if ( requestId == KBtLinkCountWatcher ) {
       
   162         //mBtLinkCountKey.Cancel();
       
   163     }
       
   164 }
       
   165 
       
   166 void BtLocalSetting::HandleError( CBtSimpleActive* active, TInt error ) {
       
   167     Q_UNUSED( active );
       
   168     Q_UNUSED( error );
       
   169 }
       
   170 
       
   171 /*!
       
   172     Update local Bluetooth device name in the data store.
       
   173     @param name the latest Bluetooth name.
       
   174  */
       
   175 void BtLocalSetting::updateDeviceName( const QString &name ) 
       
   176 {
       
   177     // To-do: the data structure initialization is not impled yet in the model
       
   178     BtuiModelDataItem& item = 
       
   179             mData[ BtuiModel::BluetoothNameCol ];
       
   180     
       
   181     if ( item.isEmpty() ) {
       
   182         // Initialize with additional information on the setting
       
   183         item[ BtuiModel::SettingIdentity ] = QVariant( tr( "Local Bluetooth name" ) );
       
   184     }
       
   185     
       
   186     bool setByUser = !name.isEmpty();
       
   187     
       
   188     // The additional parameter is the flag indicating whether the 
       
   189     // Bluetooth name has been set by the user.
       
   190     // The flag is set to true if the name has been set.    
       
   191     // requirement does not 
       
   192     //nitem[ BtuiModel::SettingValueParam ] = QVariant( setByUser );
       
   193     
       
   194     QString resolvedName( name );
       
   195     if ( resolvedName.isEmpty() ) {
       
   196         // We get the default name as suggestion for the user to set.
       
   197         getNameFromRegistry( resolvedName );
       
   198     }
       
   199     item[ BtuiModel::settingDisplay ] = QVariant( resolvedName );
       
   200     item[ BtuiModel::SettingValue ] = QVariant( resolvedName );
       
   201 }
       
   202 
       
   203 /*!
       
   204     Updates all values related to the power setting.
       
   205  */
       
   206 void BtLocalSetting::setPowerSetting( TBTPowerStateValue state )
       
   207 {
       
   208     BtuiModelDataItem& item = 
       
   209             mData[ BtuiModel::PowerStateCol ];
       
   210     if ( item.isEmpty() ) {
       
   211         // Initialize with additional information on the setting
       
   212         item[ BtuiModel::SettingIdentity ] = QVariant( tr( "Bluetooth power" ) );
       
   213     }
       
   214     
       
   215     bool powerOn = ( state == EBTPowerOn );
       
   216 
       
   217     item[ BtuiModel::settingDisplay ] = 
       
   218             powerOn ? QVariant( tr( "On" ) ) : QVariant( tr( "Off" ) );
       
   219     item[ BtuiModel::SettingValue ] = QVariant( powerOn );
       
   220 }
       
   221 
       
   222 void BtLocalSetting::setVisibilityMode( TBTVisibilityMode state )
       
   223 {
       
   224     BtuiModelDataItem& item = mData[ BtuiModel::VisibilityCol ];
       
   225 
       
   226     if ( item.isEmpty() ) {
       
   227         item[ BtuiModel::SettingIdentity ] = QVariant( tr( "Phone visibility" ) );
       
   228     }
       
   229     
       
   230     if ( state == EBTVisibilityModeHidden )
       
   231         {
       
   232         item [ BtuiModel::settingDisplay ] = QVariant( tr( "Hidden" ) );
       
   233         }
       
   234     else if ( state == EBTVisibilityModeGeneral )
       
   235         {
       
   236         item [ BtuiModel::settingDisplay ] = QVariant( tr( "Visible" ) );
       
   237         }
       
   238     else
       
   239         {
       
   240         item [ BtuiModel::settingDisplay ] = QVariant( tr( "Temporarily visible" ) );
       
   241         }
       
   242     item [ BtuiModel::SettingValue ] = QVariant( QtVisibilityMode(state) );
       
   243 }
       
   244 
       
   245 /*!
       
   246     Get local Bluetooth device name from BTRegistry.
       
   247  */
       
   248 void BtLocalSetting::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 }