16 */ |
16 */ |
17 |
17 |
18 // System includes |
18 // System includes |
19 #include <QStringList> |
19 #include <QStringList> |
20 |
20 |
|
21 // User includes |
21 #include "radiostationmodel.h" |
22 #include "radiostationmodel.h" |
22 #include "radiostationmodel_p.h" |
23 #include "radiostationmodel_p.h" |
23 #include "radiopresetstorage.h" |
24 #include "radiopresetstorage.h" |
24 #include "radioenginewrapper.h" |
25 #include "radioenginewrapper.h" |
25 #include "radiouiengine.h" |
26 #include "radiouiengine.h" |
139 */ |
143 */ |
140 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role ) |
144 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role ) |
141 { |
145 { |
142 Q_UNUSED( index ); |
146 Q_UNUSED( index ); |
143 |
147 |
144 if ( role == RadioStationModel::ToggleFavoriteRole ) { |
148 if ( role == RadioRole::ToggleFavoriteRole ) { |
145 const uint frequency = value.toUInt(); |
149 const uint frequency = value.toUInt(); |
146 RadioStation station; |
150 RadioStation station; |
147 if ( findFrequency( frequency, station ) ) { |
151 if ( findFrequency( frequency, station ) ) { |
148 setFavoriteByPreset( station.presetIndex(), !station.isFavorite() ); |
152 setFavoriteByPreset( station.presetIndex(), !station.isFavorite() ); |
149 } else { |
153 } else { |
162 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper ) |
166 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper ) |
163 { |
167 { |
164 Q_D( RadioStationModel ); |
168 Q_D( RadioStationModel ); |
165 d->mPresetStorage = storage; |
169 d->mPresetStorage = storage; |
166 d->mWrapper = wrapper; |
170 d->mWrapper = wrapper; |
167 const int presetCount = d->mPresetStorage->presetCount(); |
171 |
168 int index = d->mPresetStorage->firstPreset(); |
172 int index = d->mPresetStorage->firstPreset(); |
169 LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index ); |
173 LOG_FORMAT( "RadioStationModel::initialize: presetCount: %d, firstIndex: %d", |
170 |
174 d->mPresetStorage->presetCount(), index ); |
171 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY |
175 |
172 while ( index >= 0 ) { |
176 while ( index >= 0 ) { |
173 #else |
|
174 index = 0; |
|
175 while ( index < presetCount ) { |
|
176 #endif // COMPILE_WITH_NEW_PRESET_UTILITY |
|
177 |
|
178 RadioStation station; |
177 RadioStation station; |
179 station.detach(); |
178 |
180 |
179 RadioStationIf* stationInterface = static_cast<RadioStationIf*>( station.data_ptr() ); |
181 RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() ); |
180 if ( d->mPresetStorage->readPreset( index, *stationInterface ) ) { |
182 if ( d->mPresetStorage->readPreset( index, *preset ) ) { |
181 if ( station.isValid() && d->mWrapper->isFrequencyValid( station.frequency() ) ) { |
183 if ( station.isValid() ) { |
|
184 d->mStations.insert( station.frequency(), station ); |
182 d->mStations.insert( station.frequency(), station ); |
185 } else { |
183 } else { |
186 LOG( "RadioStationModelPrivate::initialize: Invalid station!" ); |
184 LOG( "RadioStationModel::initialize: Invalid station!" ); |
|
185 LOG_FORMAT( "Invalid station freq: %d", station.frequency() ); |
187 } |
186 } |
188 } |
187 } |
189 |
188 |
190 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY |
|
191 index = d->mPresetStorage->nextPreset( index ); |
189 index = d->mPresetStorage->nextPreset( index ); |
192 #endif |
|
193 } |
190 } |
194 |
191 |
195 d->setCurrentStation( d->mWrapper->currentFrequency() ); |
192 d->setCurrentStation( d->mWrapper->currentFrequency() ); |
196 |
193 |
197 wrapper->addObserver( d ); |
194 wrapper->addObserver( d ); |
231 RadioStation RadioStationModel::stationAt( int index ) const |
228 RadioStation RadioStationModel::stationAt( int index ) const |
232 { |
229 { |
233 // Get the value from the keys list instead of directly accessing the values list |
230 // Get the value from the keys list instead of directly accessing the values list |
234 // because QMap may have added a default-constructed value to the values list |
231 // because QMap may have added a default-constructed value to the values list |
235 Q_D( const RadioStationModel ); |
232 Q_D( const RadioStationModel ); |
236 if ( index < d->mStations.keys().count() ) { |
233 if ( index >= 0 && index < d->mStations.keys().count() ) { |
237 uint frequency = d->mStations.keys().at( index ); |
234 uint frequency = d->mStations.keys().at( index ); |
238 return d->mStations.value( frequency ); |
235 return d->mStations.value( frequency ); |
239 } |
236 } |
240 return RadioStation(); |
237 return RadioStation(); |
241 } |
238 } |
242 |
239 |
243 /*! |
240 /*! |
244 * Finds a station by frequency |
241 * Finds a station by frequency |
245 */ |
242 */ |
246 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) |
243 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station, FindCriteria::Criteria criteria ) const |
247 { |
244 { |
248 Q_D( RadioStationModel ); |
245 Q_D( const RadioStationModel ); |
|
246 |
|
247 if ( criteria == FindCriteria::IncludeManualStation && d->mCurrentStation->frequency() == frequency ) { |
|
248 station = *d->mCurrentStation; |
|
249 return true; |
|
250 } |
|
251 |
249 if ( d->mStations.contains( frequency ) ) { |
252 if ( d->mStations.contains( frequency ) ) { |
250 station = d->mStations.value( frequency ); |
253 station = d->mStations.value( frequency ); |
251 return true; |
254 return true; |
252 } |
255 } |
253 return false; |
256 return false; |
|
257 } |
|
258 |
|
259 /*! |
|
260 * Convenience function to find a radio station. |
|
261 */ |
|
262 RadioStation RadioStationModel::findStation( uint frequency, FindCriteria::Criteria criteria ) const |
|
263 { |
|
264 RadioStation station; |
|
265 findFrequency( frequency, station, criteria ); // Return value ignored |
|
266 return station; |
254 } |
267 } |
255 |
268 |
256 /*! |
269 /*! |
257 * Finds a station by preset index |
270 * Finds a station by preset index |
258 */ |
271 */ |
290 { |
303 { |
291 Q_D( RadioStationModel ); |
304 Q_D( RadioStationModel ); |
292 const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite; |
305 const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite; |
293 const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite; |
306 const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite; |
294 QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values(); |
307 QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values(); |
|
308 |
|
309 if ( list.isEmpty() ) { |
|
310 return RadioStation(); |
|
311 } |
295 |
312 |
296 // Find the previous and next station from current frequency |
313 // Find the previous and next station from current frequency |
297 RadioStation previous; |
314 RadioStation previous; |
298 RadioStation next; |
315 RadioStation next; |
299 foreach( const RadioStation& station, list ) { |
316 foreach( const RadioStation& station, list ) { |
318 |
335 |
319 return findNext ? next : previous; |
336 return findNext ? next : previous; |
320 } |
337 } |
321 |
338 |
322 /*! |
339 /*! |
|
340 * Checks if the model contains the given frequency |
|
341 */ |
|
342 bool RadioStationModel::contains( const uint frequency ) const |
|
343 { |
|
344 RadioStation unused; |
|
345 return findFrequency( frequency, unused ); |
|
346 } |
|
347 |
|
348 /*! |
323 * Removes a station by frequency |
349 * Removes a station by frequency |
324 */ |
350 */ |
325 void RadioStationModel::removeByFrequency( uint frequency ) |
351 void RadioStationModel::removeByFrequency( uint frequency ) |
326 { |
352 { |
327 RadioStation station; |
353 RadioStation station; |
359 } |
385 } |
360 |
386 |
361 // Copy the station to a temporary variable that can be used as signal parameter |
387 // Copy the station to a temporary variable that can be used as signal parameter |
362 RadioStation tempStation = station; |
388 RadioStation tempStation = station; |
363 |
389 |
364 const int row = modelIndexFromFrequency( tempStation.frequency() ).row(); |
390 const int row = indexFromFrequency( tempStation.frequency() ); |
365 beginRemoveRows( QModelIndex(), row, row ); |
391 beginRemoveRows( QModelIndex(), row, row ); |
366 |
392 |
367 d->mPresetStorage->deletePreset( tempStation.presetIndex() ); |
393 d->mPresetStorage->deletePreset( tempStation.presetIndex() ); |
368 d->mStations.remove( frequency ); |
394 d->mStations.remove( frequency ); |
369 |
395 |
412 saveStation( newStation ); |
438 saveStation( newStation ); |
413 } |
439 } |
414 } |
440 } |
415 } |
441 } |
416 } |
442 } |
417 |
|
418 reset(); // TODO: Remove. this is a workaround to HbGridView update problem |
|
419 } |
443 } |
420 |
444 |
421 /*! |
445 /*! |
422 * Adds a new station to the list |
446 * Adds a new station to the list |
423 */ |
447 */ |
424 void RadioStationModel::addStation( const RadioStation& station ) |
448 void RadioStationModel::addStation( const RadioStation& station ) |
425 { |
449 { |
426 Q_D( RadioStationModel ); |
450 Q_D( RadioStationModel ); |
427 const int newIndex = findUnusedPresetIndex(); |
451 const int newIndex = findUnusedPresetIndex(); |
428 LOG_FORMAT( "RadioStationModelPrivate::addStation: Adding station to index %d", newIndex ); |
452 LOG_FORMAT( "RadioStationModel::addStation: Adding station to index %d", newIndex ); |
429 |
453 |
430 RadioStation newStation = station; |
454 RadioStation newStation = station; |
431 newStation.setPresetIndex( newIndex ); |
455 newStation.setPresetIndex( newIndex ); |
432 newStation.unsetType( RadioStation::Temporary ); |
456 newStation.unsetType( RadioStation::ManualStation ); |
433 |
457 |
434 // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where |
458 // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where |
435 // the new station will go in the sorted frequency order |
459 // the new station will go in the sorted frequency order |
436 int row = 0; |
460 int row = 0; |
437 const int count = rowCount(); |
461 const int count = rowCount(); |
447 if ( station.frequency() > existingFreq ) { |
471 if ( station.frequency() > existingFreq ) { |
448 row = 1; |
472 row = 1; |
449 } |
473 } |
450 } |
474 } |
451 |
475 |
452 // emit layoutAboutToBeChanged(); |
|
453 beginInsertRows( QModelIndex(), row, row ); |
476 beginInsertRows( QModelIndex(), row, row ); |
454 |
477 |
455 d->doSaveStation( newStation ); |
478 d->doSaveStation( newStation ); |
456 |
479 |
457 d->setCurrentStation( d->mWrapper->currentFrequency() ); |
480 d->setCurrentStation( d->mWrapper->currentFrequency() ); |
458 |
481 |
459 endInsertRows(); |
482 endInsertRows(); |
460 |
483 |
461 // emit layoutChanged(); |
484 // Not all UI components listen to rowsInserted() signal so emit the favorite signal |
|
485 if ( newStation.isFavorite() ) { |
|
486 emit favoriteChanged( *d->mCurrentStation ); |
|
487 } |
462 } |
488 } |
463 |
489 |
464 /*! |
490 /*! |
465 * Saves the given station. It is expected to already exist in the list |
491 * Saves the given station. It is expected to already exist in the list |
466 */ |
492 */ |
469 Q_D( RadioStationModel ); |
495 Q_D( RadioStationModel ); |
470 const bool stationHasChanged = station.hasChanged(); |
496 const bool stationHasChanged = station.hasChanged(); |
471 RadioStation::Change changeFlags = station.changeFlags(); |
497 RadioStation::Change changeFlags = station.changeFlags(); |
472 station.resetChangeFlags(); |
498 station.resetChangeFlags(); |
473 |
499 |
474 if ( station.isType( RadioStation::Temporary ) ) { |
500 if ( station.isType( RadioStation::ManualStation ) ) { |
475 |
501 |
|
502 d->mManualStation = station; |
476 emitChangeSignals( station, changeFlags ); |
503 emitChangeSignals( station, changeFlags ); |
477 |
504 |
478 } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) { |
505 } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) { |
479 |
506 |
480 d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) ); |
507 d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) ); |
499 */ |
526 */ |
500 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite ) |
527 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite ) |
501 { |
528 { |
502 Q_D( RadioStationModel ); |
529 Q_D( RadioStationModel ); |
503 if ( d->mWrapper->isFrequencyValid( frequency ) ) { |
530 if ( d->mWrapper->isFrequencyValid( frequency ) ) { |
504 LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByFrequency, frequency: %d", frequency ); |
531 LOG_FORMAT( "RadioStationModel::setFavoriteByFrequency, frequency: %d", frequency ); |
505 RadioStation station; |
532 RadioStation station; |
506 if ( findFrequency( frequency, station ) ) { // Update existing preset |
533 if ( findFrequency( frequency, station ) ) { // Update existing preset |
507 if ( station.isFavorite() != favorite ) { |
534 if ( station.isFavorite() != favorite ) { |
508 station.setFavorite( favorite ); |
535 station.setFavorite( favorite ); |
509 saveStation( station ); |
536 saveStation( station ); |
517 newStation.setFrequency( frequency ); |
544 newStation.setFrequency( frequency ); |
518 } |
545 } |
519 |
546 |
520 newStation.setType( RadioStation::LocalStation | RadioStation::Favorite ); |
547 newStation.setType( RadioStation::LocalStation | RadioStation::Favorite ); |
521 |
548 |
522 // If PI code has been received, it is a local station |
|
523 if ( newStation.hasPiCode() ) { |
|
524 newStation.setType( RadioStation::LocalStation ); |
|
525 } |
|
526 |
|
527 // Emit the signals only after adding the preset and reinitializing the current station |
549 // Emit the signals only after adding the preset and reinitializing the current station |
528 // because the UI will probably query the current station in its slots that get called. |
550 // because the UI will probably query the current station in its slots that get called. |
529 addStation( newStation ); |
551 addStation( newStation ); |
530 } |
552 } |
531 } |
553 } |
534 /*! |
556 /*! |
535 * Changes the favorite status of a station by its preset index |
557 * Changes the favorite status of a station by its preset index |
536 */ |
558 */ |
537 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite ) |
559 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite ) |
538 { |
560 { |
539 LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex ); |
561 LOG_FORMAT( "RadioStationModel::setFavoriteByPreset, presetIndex: %d", presetIndex ); |
540 RadioStation station; |
562 RadioStation station; |
541 if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
563 if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
542 station.setFavorite( favorite ); |
564 station.setFavorite( favorite ); |
543 saveStation( station ); |
565 saveStation( station ); |
544 } |
566 } |
547 /*! |
569 /*! |
548 * Renames a station by its preset index |
570 * Renames a station by its preset index |
549 */ |
571 */ |
550 void RadioStationModel::renameStation( int presetIndex, const QString& name ) |
572 void RadioStationModel::renameStation( int presetIndex, const QString& name ) |
551 { |
573 { |
552 LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) ); |
574 LOG_FORMAT( "RadioStationModel::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) ); |
553 RadioStation station; |
575 RadioStation station; |
554 if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
576 if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) { |
555 station.setUserDefinedName( name ); |
577 station.setUserDefinedName( name ); |
556 saveStation( station ); |
578 saveStation( station ); |
557 } |
579 } |
613 } |
635 } |
614 |
636 |
615 /*! |
637 /*! |
616 * Returns the model index corresponding to the given frequency |
638 * Returns the model index corresponding to the given frequency |
617 */ |
639 */ |
618 QModelIndex RadioStationModel::modelIndexFromFrequency( uint frequency ) |
640 int RadioStationModel::indexFromFrequency( uint frequency ) |
619 { |
641 { |
620 RadioStation station; |
642 RadioStation station; |
621 if ( findFrequency( frequency, station ) ) { |
643 if ( findFrequency( frequency, station ) ) { |
622 return index( findPresetIndex( station.presetIndex() ), 0 ); |
644 return findPresetIndex( station.presetIndex() ); |
623 } |
645 } |
624 return QModelIndex(); |
646 return -1; |
625 } |
647 } |
626 |
648 |
627 /*! |
649 /*! |
628 * Private slot |
650 * Private slot |
629 * Timer timeout slot to indicate that the dynamic PS check has ended |
651 * Timer timeout slot to indicate that the dynamic PS check has ended |
704 int index = 0; |
726 int index = 0; |
705 for ( ; indexes.contains( index ); ++index ) { |
727 for ( ; indexes.contains( index ); ++index ) { |
706 // Nothing to do here |
728 // Nothing to do here |
707 } |
729 } |
708 |
730 |
709 LOG_FORMAT( "RadioStationModelPrivate::findUnusedPresetIndex, index: %d", index ); |
731 LOG_FORMAT( "RadioStationModel::findUnusedPresetIndex, index: %d", index ); |
710 return index; |
732 return index; |
711 } |
733 } |
712 |
|
713 /*! |
|
714 * Used by the RDS data setters to find the correct station where the data is set |
|
715 */ |
|
716 RadioStation RadioStationModel::findCurrentStation( uint frequency ) |
|
717 { |
|
718 Q_D( RadioStationModel ); |
|
719 RadioStation station = *d->mCurrentStation; |
|
720 if ( station.frequency() != frequency ) { |
|
721 if ( !findFrequency( frequency, station ) ) { |
|
722 return RadioStation(); |
|
723 } |
|
724 } |
|
725 return station; |
|
726 } |
|