1 /* |
|
2 * Copyright (c) 2009 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 "usbuimodelactive.h" |
|
20 #include "mydebug.h" |
|
21 #include <QStringList> |
|
22 #include <UsbWatcherInternalCRKeys.h> |
|
23 #include <XQSettingsKey> |
|
24 #include <usbman.h> |
|
25 |
|
26 const QString TextIdPrefix = ("txt_usb_"); |
|
27 const QString ModeIconNamePrefix = (":/icons/usb_icon_mode_"); |
|
28 |
|
29 /*! |
|
30 Constructor. |
|
31 */ |
|
32 UsbUiSettingModel::UsbUiSettingModel( QObject *parent ) |
|
33 : QAbstractItemModel( parent ) |
|
34 { |
|
35 mModelActive = new UsbUiModelActive(); |
|
36 |
|
37 for ( int i = 0; i < UsbUiSettingModel::EndOfSettings; i++ ) { |
|
38 // Initialize the list with empty values. |
|
39 mSettingsList.append( QMap< int, QVariant >() ); |
|
40 } |
|
41 |
|
42 mCurrentMode = currentMode(); |
|
43 mPreviousMode = mCurrentMode; |
|
44 setUsbSettings(mCurrentMode); |
|
45 bool ret = mSettingsManager.startMonitoring( |
|
46 XQSettingsKey( XQSettingsKey::TargetCentralRepository, |
|
47 KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) ); |
|
48 myDebug() << ">>> UsbUiSettingModel::startMonitoring value=" |
|
49 << ret; |
|
50 |
|
51 // signal: personality changed in the central repository |
|
52 connect( &mSettingsManager, |
|
53 SIGNAL( valueChanged( const XQSettingsKey&, const QVariant& ) ), |
|
54 this, |
|
55 SLOT( usbModeChanged( const XQSettingsKey&, const QVariant& ) ) ); |
|
56 |
|
57 // signal: response from usbwatcher to our attempt to set the personality |
|
58 connect( mModelActive, |
|
59 SIGNAL( requestCompleted( int status ) ), |
|
60 this, |
|
61 SLOT( setPersonalityCompleted (int status ))); |
|
62 |
|
63 } |
|
64 /*! |
|
65 * calls the model's set functions to initialize the model's data |
|
66 */ |
|
67 void UsbUiSettingModel::setUsbSettings( int aModeId ) |
|
68 { |
|
69 setUsbModelistSetting( aModeId ); |
|
70 setDefaultModeSetting(); |
|
71 setDescriptionSetting(); |
|
72 } |
|
73 |
|
74 /*! |
|
75 Destructor. |
|
76 */ |
|
77 UsbUiSettingModel::~UsbUiSettingModel() |
|
78 { |
|
79 myDebug() << ">>> UsbUiSettingModel::~UsbUiSettingModel"; |
|
80 mSettingsManager.stopMonitoring( |
|
81 XQSettingsKey( XQSettingsKey::TargetCentralRepository, |
|
82 KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) ); |
|
83 delete mModelActive; |
|
84 myDebug() << "<<< UsbUiSettingModel::~UsbUiSettingModel"; |
|
85 } |
|
86 |
|
87 /*! |
|
88 Provides notification of changes in selected usb mode |
|
89 */ |
|
90 void UsbUiSettingModel::usbModeChanged( const XQSettingsKey &key, |
|
91 const QVariant &value ) |
|
92 { |
|
93 Q_UNUSED(key); |
|
94 myDebug() << ">>> UsbUiSettingModel::usbModeChanged"; |
|
95 // key is not checked, as we monitor only one key |
|
96 |
|
97 setNewUsbMode(value.toInt()); |
|
98 |
|
99 myDebug() << "<<< UsbUiSettingModel::usbModeChanged"; |
|
100 } |
|
101 /*! |
|
102 * updates the model rows and emits signal datachanged |
|
103 */ |
|
104 void UsbUiSettingModel::setNewUsbMode(int newPersonality) |
|
105 |
|
106 { |
|
107 myDebug() << ">>> UsbUiSettingModel::setNewUsbMode value=" |
|
108 << newPersonality; |
|
109 |
|
110 mCurrentMode = newPersonality; |
|
111 setUsbSettings(mCurrentMode); |
|
112 |
|
113 emit dataChanged( createIndex( DefaultMode, KDefaultColumn ), |
|
114 createIndex( UsbModeList, KDefaultColumn ) ); |
|
115 |
|
116 myDebug() << "<<< UsbUiSettingModel::setNewUsbMode"; |
|
117 } |
|
118 /*! |
|
119 * Returns the index of the item in the model specified by the given row, column and parent index. |
|
120 */ |
|
121 QModelIndex UsbUiSettingModel::index( int row, int column, const QModelIndex &parent ) const |
|
122 { |
|
123 return hasIndex( row, column, parent ) ? createIndex( row, column ) : QModelIndex(); |
|
124 } |
|
125 |
|
126 |
|
127 /* |
|
128 This model does not support hierarchy, so this returns an empty model index. |
|
129 */ |
|
130 QModelIndex UsbUiSettingModel::parent( const QModelIndex &child ) const |
|
131 { |
|
132 Q_UNUSED( child ); |
|
133 return QModelIndex(); |
|
134 } |
|
135 /*! |
|
136 * Returns the number of rows under the given parent |
|
137 */ |
|
138 int UsbUiSettingModel::rowCount( const QModelIndex &parent ) const |
|
139 { |
|
140 Q_UNUSED( parent ); |
|
141 return mSettingsList.count(); |
|
142 } |
|
143 |
|
144 |
|
145 /*! |
|
146 * Returns the number of columns for the children of the given parent. |
|
147 * This model is one-dimensional, so this returns 1. |
|
148 */ |
|
149 int UsbUiSettingModel::columnCount( const QModelIndex &parent ) const |
|
150 { |
|
151 Q_UNUSED( parent ); |
|
152 return 1; |
|
153 } |
|
154 /*! |
|
155 * Returns the data stored under the given role for the item referred to by the index. |
|
156 */ |
|
157 QVariant UsbUiSettingModel::data( const QModelIndex &index, int role ) const |
|
158 { |
|
159 return mSettingsList.value( index.row() ).value( role ); |
|
160 } |
|
161 |
|
162 |
|
163 /*! |
|
164 * This is called when usb mode is changed, it only sets the personality when the given role is EditRole |
|
165 */ |
|
166 bool UsbUiSettingModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
167 { |
|
168 myDebug() << ">>> UsbUiSettingModel::setData"; |
|
169 bool success = false; |
|
170 if ( role == Qt::EditRole ) { |
|
171 // Only allow changes on the value with role EditRole. Others are managed here. |
|
172 switch ( index.row() ) { |
|
173 case UsbModeList: { |
|
174 mModelActive->SetUsbPersonality( mPersonalityIds.at( value.toInt() ) ); |
|
175 success = true; |
|
176 // also update the rest of the model with the about to be selected mode right away |
|
177 setNewUsbMode(mPersonalityIds.at( value.toInt() )); |
|
178 break; |
|
179 } |
|
180 case DefaultMode: |
|
181 //no break |
|
182 case Description: |
|
183 //no break |
|
184 default: |
|
185 myDebug() << " UsbUiSettingModel::setData not supported row " << index.row(); |
|
186 break; |
|
187 } |
|
188 } |
|
189 myDebug() << "<<< UsbUiSettingModel::setData return " << success; |
|
190 return success; |
|
191 } |
|
192 |
|
193 /*! |
|
194 * Getter for the source data. |
|
195 */ |
|
196 const QModelIndex* UsbUiSettingModel::sourceData() const |
|
197 { |
|
198 return new QModelIndex( createIndex( 0, 0 ) ); |
|
199 } |
|
200 |
|
201 /*! |
|
202 * creats the logical name of the modename string for the list |
|
203 */ |
|
204 QString UsbUiSettingModel::modeListName( int modeId ) |
|
205 { |
|
206 QString textId = TextIdPrefix + "modelistname_" + QString::number( modeId ); |
|
207 QString modeName = qtTrId( textId.toAscii() ); |
|
208 return modeName; |
|
209 } |
|
210 |
|
211 /*! |
|
212 * Get the current USB mode (personality) ID |
|
213 */ |
|
214 int UsbUiSettingModel::currentMode() |
|
215 { |
|
216 myDebug() << ">>> UsbUiSettingModel::CurrentMode"; |
|
217 int currentMode = mSettingsManager.readItemValue( |
|
218 XQSettingsKey( XQSettingsKey::TargetCentralRepository, |
|
219 KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) ).toInt(); |
|
220 myDebug() << "<<< UsbUiSettingModel::CurrentMode " << currentMode; |
|
221 return currentMode; |
|
222 } |
|
223 |
|
224 /*! |
|
225 * sets the value to different roles of the model's first row |
|
226 */ |
|
227 void UsbUiSettingModel::setDefaultModeSetting() |
|
228 { |
|
229 QString iconName; |
|
230 QString modeName; |
|
231 //empty iconName and modeName are used for a hidden personality |
|
232 if ( !isPersonalityHidden(mCurrentMode) ) { |
|
233 iconName = ModeIconNamePrefix + QString::number( mCurrentMode ) + ".svg"; |
|
234 QString textId = TextIdPrefix + "modename_" + QString::number( mCurrentMode ); |
|
235 modeName = qtTrId( textId.toAscii() ); |
|
236 } |
|
237 |
|
238 myDebug() << ">>> UsbUiSettingModel::setDefaultModeSetting iconName=" |
|
239 << iconName << " modeName=" << modeName; |
|
240 QMap< int, QVariant > val = mSettingsList.at( DefaultMode ); |
|
241 if ( val.isEmpty() ) { |
|
242 val[ SettingType ] = QVariant( DefaultMode ); |
|
243 } |
|
244 val[ Qt::DecorationRole ] = QVariant(iconName) ; |
|
245 val[ Qt::DisplayRole ]= QVariant(modeName) ; |
|
246 mSettingsList.replace( DefaultMode, val ); |
|
247 myDebug() << "<<< UsbUiSettingModel::setDefaultModeSetting"; |
|
248 } |
|
249 |
|
250 /*! |
|
251 Updates all values related to the mode description. |
|
252 */ |
|
253 void UsbUiSettingModel::setDescriptionSetting() |
|
254 { |
|
255 QString description; |
|
256 //the description will be empty for a hidden personality |
|
257 if ( !isPersonalityHidden(mCurrentMode) ) { |
|
258 QString textId = TextIdPrefix + "description_" |
|
259 + QString::number( mCurrentMode ); |
|
260 description = qtTrId( textId.toAscii() ); |
|
261 } |
|
262 myDebug() << ">>> UsbUiSettingModel::setDescriptionSetting description=" |
|
263 << description; |
|
264 QMap< int, QVariant > val = mSettingsList.at( Description ); |
|
265 if ( val.isEmpty() ) { |
|
266 |
|
267 val[ SettingType ] = QVariant( Description ); |
|
268 |
|
269 } |
|
270 // The display role stores the string representation of the actual value. |
|
271 val[ Qt::DisplayRole ] = QVariant( description ); |
|
272 mSettingsList.replace( Description, val ); |
|
273 myDebug() << "<<< UsbUiSettingModel::setDescriptionSetting"; |
|
274 } |
|
275 |
|
276 /*! |
|
277 Updates all values related to the visibility setting. |
|
278 Updates the selectable USB modes only in the 1st call. |
|
279 */ |
|
280 bool UsbUiSettingModel::setUsbModelistSetting( int aModeId ) |
|
281 { |
|
282 myDebug() << ">>> UsbUiSettingModel::setUsbModelistSetting aModeIndex=" |
|
283 << aModeId; |
|
284 bool ret = true; |
|
285 QMap< int, QVariant > val = mSettingsList.at(UsbModeList); |
|
286 if ( val.isEmpty() ) { |
|
287 val[ SettingType ] = QVariant( UsbModeList ); |
|
288 |
|
289 RUsb iUsbman; |
|
290 if ( iUsbman.Connect() == KErrNone ) { |
|
291 |
|
292 RArray<TInt> personalityIds; |
|
293 mPersonalityIds.clear(); |
|
294 if ( iUsbman.GetPersonalityIds( personalityIds ) == KErrNone ) { |
|
295 QStringList modeList; |
|
296 for ( int i = 0; i < personalityIds.Count(); i++ ) { |
|
297 if ( !isPersonalityHidden(iUsbman, personalityIds[i]) ) { |
|
298 mPersonalityIds.append( personalityIds[i] ); |
|
299 modeList.append( modeListName( mPersonalityIds[i] ) ); |
|
300 } |
|
301 } |
|
302 val[ SettingsModeNameList ] = QVariant( modeList ); |
|
303 } |
|
304 } |
|
305 else{ |
|
306 ret = false; |
|
307 } |
|
308 iUsbman.Close(); |
|
309 } |
|
310 //index will be -1 for hidden personality |
|
311 val[ Qt::EditRole ] = QVariant( mPersonalityIds.indexOf(aModeId) ); |
|
312 mSettingsList.replace( UsbModeList, val ); |
|
313 myDebug() << "<<< UsbUiSettingModel::setUsbModelistSetting"; |
|
314 return ret; |
|
315 } |
|
316 |
|
317 /*! |
|
318 * it checks the response from usbwatcher to see if the new mode change has been successful |
|
319 * it will go back to the previous personality if it has not been successfull |
|
320 */ |
|
321 void UsbUiSettingModel::setPersonalityCompleted (int status ) |
|
322 { |
|
323 myDebug() << ">>> UsbUiSettingModel::setPersonalityCompleted status= " |
|
324 << status; |
|
325 // status contains Symbian error code from usbwatcher |
|
326 // if the status is KErrNone, we are ready to process the next request |
|
327 if (status != KErrNone) |
|
328 { |
|
329 // changing the personality failed, so we need to set back the previous personality |
|
330 // the value will be read from central repository and also updates mCurrentMode |
|
331 mPreviousMode = mSettingsManager.readItemValue( |
|
332 XQSettingsKey( XQSettingsKey::TargetCentralRepository, |
|
333 KCRUidUsbWatcher.iUid, KUsbWatcherPersonality ) ).toInt(); |
|
334 setNewUsbMode(mPreviousMode); |
|
335 } |
|
336 |
|
337 // after handling the return code we know the the current personality works |
|
338 // and we will not go back to the previous one |
|
339 mPreviousMode = mCurrentMode; |
|
340 myDebug() << "<<< UsbUiSettingModel::setPersonalityCompleted"; |
|
341 } |
|
342 |
|
343 bool UsbUiSettingModel::isPersonalityHidden(RUsb &usbman, TInt personalityId) |
|
344 { |
|
345 myDebug() << ">>> UsbUiSettingModel::isPersonalityHidden from USB Manager"; |
|
346 bool hidden = false; |
|
347 TUint32 property = 0; |
|
348 TInt ret = usbman.GetPersonalityProperty(personalityId, property); |
|
349 if (ret == KErrNone) { |
|
350 myDebug() << "property " << property; |
|
351 if (property & KUsbPersonalityPropertyHidden) { |
|
352 hidden = true; |
|
353 } |
|
354 } |
|
355 myDebug() << "<<< UsbUiSettingModel::isPersonalityHidden " << hidden; |
|
356 return hidden; |
|
357 } |
|
358 |
|
359 bool UsbUiSettingModel::isPersonalityHidden(TInt personalityId) |
|
360 { |
|
361 return ( mPersonalityIds.indexOf(personalityId) == -1 ); |
|
362 } |
|