usbuis/usbsettingsapp/src/usbuisettingmodel.cpp
branchRCL_3
changeset 24 e02eb84a14d2
parent 23 25fce757be94
child 25 60826dff342d
equal deleted inserted replaced
23:25fce757be94 24:e02eb84a14d2
     1 /*
       
     2 * Copyright (c) 2009-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 "usbuisettingmodel.h"
       
    19 #include <QStringList>
       
    20 #include <UsbWatcherInternalCRKeys.h>
       
    21 #include <XQSettingsKey>
       
    22 #include <usbman.h>
       
    23 #include <HbIcon>
       
    24 #include <QItemSelection>
       
    25 #include <QItemSelectionModel>
       
    26 #include "usbuimodelactive.h"
       
    27 #include "usbdebug.h"
       
    28 
       
    29 
       
    30 const QString TextIdPrefix = ("txt_usb_dblist_");
       
    31 const QString DescriptionIdPostfix = ("_val");
       
    32 const QString ModeIconNamePrefix = ("qtg_large_");
       
    33 
       
    34 /*!
       
    35     Constructor.
       
    36  */
       
    37 UsbUiSettingModel::UsbUiSettingModel( QObject *parent )
       
    38     : QAbstractItemModel( parent), mSelectionModel(NULL)
       
    39 {
       
    40     mModelActive = new UsbUiModelActive();
       
    41     mCurrentMode = currentMode();
       
    42     initializeModelData(mCurrentMode);
       
    43   	bool ret = mSettingsManager.startMonitoring( 
       
    44    	        XQSettingsKey( XQSettingsKey::TargetCentralRepository, 
       
    45    	                KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) );
       
    46     myDebug() << ">>> UsbUiSettingModel::startMonitoring value=" 
       
    47               << ret; 
       
    48                               
       
    49    	// signal: personality changed in the central repository                
       
    50    	ret = connect( &mSettingsManager, 
       
    51    	        SIGNAL( valueChanged( const XQSettingsKey&, const QVariant& ) ), 
       
    52    	        this, 
       
    53    	        SLOT( cenrepChanged( const XQSettingsKey&, const QVariant& ) ) );
       
    54     myDebug() << ">>> UsbUiSettingModel::UsbUiSettingModel connect valueChanged="
       
    55             << ret;
       
    56    	        
       
    57     // signal: response from usbwatcher to our attempt to set the personality 	        
       
    58    	ret = connect( mModelActive, 
       
    59    	        SIGNAL( requestCompleted( int ) ), 
       
    60    	        this, 
       
    61    	        SLOT( personalitySetCompleted( int )));
       
    62     myDebug() << ">>> UsbUiSettingModel::UsbUiSettingModel connect requestCompleted="
       
    63             << ret;
       
    64 }
       
    65 
       
    66 /*!
       
    67     Destructor.
       
    68  */
       
    69 UsbUiSettingModel::~UsbUiSettingModel()
       
    70 {
       
    71     myDebug() << ">>> UsbUiSettingModel::~UsbUiSettingModel";
       
    72     mSettingsManager.stopMonitoring( 
       
    73             XQSettingsKey( XQSettingsKey::TargetCentralRepository, 
       
    74                     KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) );
       
    75     delete mModelActive;
       
    76     myDebug() << "<<< UsbUiSettingModel::~UsbUiSettingModel";
       
    77 }
       
    78 
       
    79 /*!
       
    80     Provides notification of changes in selected usb mode
       
    81  */
       
    82 void UsbUiSettingModel::cenrepChanged( const XQSettingsKey &key,  
       
    83         const QVariant &value )  
       
    84 {
       
    85     Q_UNUSED(key);
       
    86     myDebug() << ">>> UsbUiSettingModel::cenrepChanged";
       
    87     // key is not checked, as we monitor only one key
       
    88     updateSelectionModel(value.toInt());                                 
       
    89     myDebug() << "<<< UsbUiSettingModel::cenrepChanged"; 
       
    90 }
       
    91 
       
    92 /*!
       
    93  * updates the selection model
       
    94  * The selection model will signal the view.
       
    95  */
       
    96 void UsbUiSettingModel::updateSelectionModel(int newPersonality)
       
    97 {
       
    98     myDebug() << ">>> UsbUiSettingModel::updateSelectionModel value=" 
       
    99             << newPersonality; 
       
   100             
       
   101     mCurrentMode = newPersonality;
       
   102     mSelectionModel->clear();    
       
   103     int row = mPersonalityIds.indexOf(newPersonality);
       
   104     myDebug() << ">>> UsbUiSettingModel::updateSelectionModel row=" 
       
   105             << row; 
       
   106     // in case of the hidden personality , the selection model is left empty
       
   107     if ( row >= 0 ) {
       
   108         //set selection model for the new selection
       
   109         QModelIndex selectionIndex = index(row, 0, QModelIndex());
       
   110         QItemSelection selection(selectionIndex, selectionIndex);
       
   111         mSelectionModel->select(selection, QItemSelectionModel::Select);    
       
   112     }
       
   113     myDebug() << "<<< UsbUiSettingModel::updateSelectionModel"; 
       
   114 }
       
   115 
       
   116 /*!
       
   117  * Returns the index of the item in the model specified by the given row, column and parent index.
       
   118  */
       
   119 QModelIndex UsbUiSettingModel::index( int row, int column, const QModelIndex &parent ) const
       
   120 {
       
   121     return hasIndex( row, column, parent ) ? createIndex( row, column ) : QModelIndex();
       
   122 }
       
   123 
       
   124 /*
       
   125     This model does not support hierarchy, so this returns an empty model index. 
       
   126  */
       
   127 QModelIndex UsbUiSettingModel::parent( const QModelIndex &child ) const
       
   128 {
       
   129     Q_UNUSED( child );
       
   130     return QModelIndex();
       
   131 }
       
   132 
       
   133 /*!
       
   134  * Returns the number of rows under the given parent
       
   135  */
       
   136 int UsbUiSettingModel::rowCount( const QModelIndex &parent ) const
       
   137 {
       
   138     Q_UNUSED( parent );
       
   139     return mSettingsList.count();
       
   140 }
       
   141 
       
   142 /*!
       
   143  * Returns the number of columns for the children of the given parent.
       
   144  * This model is one-dimensional, so this returns 1.
       
   145  */
       
   146 int UsbUiSettingModel::columnCount( const QModelIndex &parent ) const
       
   147 {
       
   148     Q_UNUSED( parent );
       
   149     return 1;
       
   150 }
       
   151 
       
   152 /*!
       
   153  * Returns the data stored under the given role for the item referred to by the index.
       
   154  */
       
   155 QVariant UsbUiSettingModel::data( const QModelIndex &index, int role ) const
       
   156 {
       
   157     return mSettingsList.value( index.row() ).value( role );
       
   158 }
       
   159 
       
   160 /*!
       
   161  * This is called when usb selection is changed in the view (selection model). 
       
   162  */
       
   163 void UsbUiSettingModel::handleSelectionChange(const QItemSelection &selected, 
       
   164         const QItemSelection &deselected )
       
   165 {
       
   166     Q_UNUSED( deselected );
       
   167     myDebug() << ">>>  UsbUiSettingModel::handleSelectionChange";
       
   168     QModelIndexList items = selected.indexes();
       
   169     if (!items.isEmpty()) {
       
   170         myDebug() << "     UsbUiSettingModel::handleSelectionChange item exists";
       
   171         QModelIndex index = items[0];
       
   172         int newPersonalityId = mPersonalityIds.at(index.row());
       
   173         if ( newPersonalityId != mCurrentMode ) {
       
   174             myDebug() << "     UsbUiSettingModel::handleSelectionChange setting personality";
       
   175             mModelActive->SetUsbPersonality(newPersonalityId);
       
   176         }
       
   177     }
       
   178     myDebug() << "<<<  UsbUiSettingModel::handleSelectionChange return";
       
   179 }
       
   180 
       
   181 /*!
       
   182  * Getter for the source data.
       
   183  */
       
   184 const QModelIndex* UsbUiSettingModel::sourceData() const
       
   185 {
       
   186     return new QModelIndex( createIndex( 0, 0 ) );
       
   187 }
       
   188 
       
   189 void  UsbUiSettingModel::setSelectionModel(QItemSelectionModel *selectionModel)
       
   190     {
       
   191     myDebug() << ">>>  UsbUiSettingModel::setSelectionModel";
       
   192     mSelectionModel = selectionModel;
       
   193     connect( mSelectionModel, 
       
   194                 SIGNAL( selectionChanged( const QItemSelection &, const QItemSelection & ) ), 
       
   195                 this, 
       
   196                 SLOT( handleSelectionChange( const QItemSelection &, const QItemSelection & ) ) );
       
   197     updateSelectionModel(mCurrentMode);
       
   198     myDebug() << "<<<  UsbUiSettingModel::setSelectionModel return";
       
   199     }
       
   200 
       
   201 /*!
       
   202  * Get the translated mode name for the personality friendly name.
       
   203  */
       
   204 QString UsbUiSettingModel::modeName( QString &friendlyName )
       
   205 {
       
   206     myDebug() << ">>>  UsbUiSettingModel::modeName";
       
   207     QString textId = TextIdPrefix + friendlyName;
       
   208     QString modeName = hbTrId( textId.toAscii() );
       
   209     myDebug() << "<<< UsbUiSettingModel::modeName " << modeName;
       
   210     return modeName;
       
   211 }
       
   212 
       
   213 /*!
       
   214  * Get the current USB mode (personality) ID
       
   215  */
       
   216 int UsbUiSettingModel::currentMode()
       
   217 {
       
   218     myDebug() << ">>>  UsbUiSettingModel::CurrentMode";
       
   219     int currentMode = mSettingsManager.readItemValue(
       
   220                 XQSettingsKey( XQSettingsKey::TargetCentralRepository, 
       
   221                 KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) ).toInt();
       
   222     myDebug() << "<<< UsbUiSettingModel::CurrentMode " << currentMode;
       
   223     return currentMode;
       
   224 }
       
   225 
       
   226 void UsbUiSettingModel::initializeModelData( int aModeId )
       
   227 {
       
   228     myDebug() << ">>> UsbUiSettingModel::initializeModelData aModeId="
       
   229             << aModeId;
       
   230     RUsb usbMan;
       
   231     if ( usbMan.Connect() == KErrNone ) {
       
   232         RArray<TInt> personalityIds;
       
   233         mPersonalityIds.clear();
       
   234         if ( usbMan.GetPersonalityIds( personalityIds ) == KErrNone ) {
       
   235             for ( int i = 0; i < personalityIds.Count(); i++ ) {
       
   236                 myDebug() << ">>> UsbUiSettingModel::initializeModelData personality ID ="
       
   237                     << personalityIds[i];
       
   238                 if ( !isPersonalityHidden(usbMan, personalityIds[i]) ) {
       
   239                     mPersonalityIds.append( personalityIds[i] );                    
       
   240                     QString friendlyName = getFriendlyName(usbMan, personalityIds[i]);
       
   241                     
       
   242                     QStringList displayList;
       
   243                     //text-1 mode name
       
   244                     displayList.append( modeName( friendlyName ) );
       
   245                     
       
   246                     //text-2 description
       
   247                     QString textId = TextIdPrefix + friendlyName + DescriptionIdPostfix;
       
   248                     displayList.append( hbTrId(textId.toAscii()) );
       
   249                     
       
   250                     QMap< int, QVariant > dataRow;
       
   251                     dataRow[ Qt::DisplayRole ] = QVariant( displayList );
       
   252                     
       
   253                     //icon-1
       
   254                     QString iconName = ModeIconNamePrefix + friendlyName;
       
   255                     HbIcon icon(iconName);
       
   256                     QList<QVariant> icons;
       
   257                     icons << icon;                    
       
   258                     dataRow[ Qt::DecorationRole ] = QVariant( icons );
       
   259                     
       
   260                     mSettingsList << dataRow;
       
   261                 }
       
   262             }
       
   263         }
       
   264         personalityIds.Close();
       
   265         usbMan.Close();
       
   266     }
       
   267     myDebug() << "<<< UsbUiSettingModel::initializeModelData";
       
   268 }
       
   269 
       
   270 /*!
       
   271  * it checks the response from usbwatcher to see if the new mode change has been successful
       
   272  * it will go back to the previous personality if it has not been successful
       
   273  */
       
   274 void UsbUiSettingModel::personalitySetCompleted (int status )
       
   275 {
       
   276     myDebug() << ">>> UsbUiSettingModel::personalitySetCompleted status= "
       
   277         << status;      
       
   278     // status contains Symbian error code from usbwatcher
       
   279     // if the status is KErrNone, we are ready to process the next request
       
   280     if (status != KErrNone) {
       
   281         // changing the personality failed, so we need to set back the previous personality
       
   282         // the value will be read from central repository and also updates mCurrentMode  
       
   283         updateSelectionModel(currentMode());
       
   284     }
       
   285    
       
   286     myDebug() << "<<< UsbUiSettingModel::personalitySetCompleted";      
       
   287 }
       
   288 
       
   289 bool UsbUiSettingModel::isPersonalityHidden(RUsb &usbman, TInt personalityId)
       
   290 {
       
   291     myDebug() << ">>> UsbUiSettingModel::isPersonalityHidden from USB Manager";
       
   292     bool hidden = false;
       
   293     TUint32 property = 0;
       
   294     TInt ret = usbman.GetPersonalityProperty(personalityId, property);
       
   295     if (ret == KErrNone) {
       
   296         myDebug() << "property " << property;
       
   297         if (property & KUsbPersonalityPropertyHidden) {
       
   298             hidden = true;
       
   299         }
       
   300     } 
       
   301     myDebug() << "<<< UsbUiSettingModel::isPersonalityHidden " << hidden;
       
   302     return hidden;
       
   303 }
       
   304 
       
   305 QString UsbUiSettingModel::getFriendlyName(RUsb &usbman, TInt personalityId)
       
   306 {
       
   307     myDebug() << ">>> UsbUiSettingModel::getFriendlyName";
       
   308     QString friendlyName;
       
   309     HBufC* description = NULL;
       
   310     TInt err = usbman.GetDescription(personalityId, description);
       
   311     if (err == KErrNone) {
       
   312         friendlyName = QString::fromUtf16(description->Ptr(), description->Length());
       
   313         friendlyName.replace( QChar(' '), QChar('_') );
       
   314         delete description;
       
   315     } else {
       
   316         myDebug() << "    UsbUiSettingModel::getFriendlyName RUsb error "
       
   317             << err;    
       
   318     }
       
   319     myDebug() << "    UsbUiSettingModel::getFriendlyName friendlyName=" << friendlyName;
       
   320     myDebug() << "<<< UsbUiSettingModel::getFriendlyName";
       
   321     return friendlyName;
       
   322 }