radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 34 bc10a61bd7d3
parent 28 075425b8d9a4
child 36 ba22309243a1
equal deleted inserted replaced
28:075425b8d9a4 34:bc10a61bd7d3
    61 /*!
    61 /*!
    62  *
    62  *
    63  */
    63  */
    64 RadioStationModel::~RadioStationModel()
    64 RadioStationModel::~RadioStationModel()
    65 {
    65 {
    66     delete d_ptr;
       
    67 }
    66 }
    68 
    67 
    69 /*!
    68 /*!
    70  *
    69  *
    71  */
    70  */
   169     Q_D( RadioStationModel );
   168     Q_D( RadioStationModel );
   170     d->mPresetStorage = storage;
   169     d->mPresetStorage = storage;
   171     d->mWrapper = wrapper;
   170     d->mWrapper = wrapper;
   172 
   171 
   173     int index = d->mPresetStorage->firstPreset();
   172     int index = d->mPresetStorage->firstPreset();
   174     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d",
   173     LOG_FORMAT( "RadioStationModel::initialize: presetCount: %d, firstIndex: %d",
   175                                             d->mPresetStorage->presetCount(), index );
   174                                             d->mPresetStorage->presetCount(), index );
   176 
   175 
   177     while ( index >= 0 ) {
   176     while ( index >= 0 ) {
   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         index = d->mPresetStorage->nextPreset( index );
   189         index = d->mPresetStorage->nextPreset( index );
   191     }
   190     }
   239 }
   238 }
   240 
   239 
   241 /*!
   240 /*!
   242  * Finds a station by frequency
   241  * Finds a station by frequency
   243  */
   242  */
   244 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) const
   243 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station, FindCriteria::Criteria criteria ) const
   245 {
   244 {
   246     Q_D( const 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 
   247     if ( d->mStations.contains( frequency ) ) {
   252     if ( d->mStations.contains( frequency ) ) {
   248         station = d->mStations.value( frequency );
   253         station = d->mStations.value( frequency );
   249         return true;
   254         return true;
   250     }
   255     }
   251     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;
   252 }
   267 }
   253 
   268 
   254 /*!
   269 /*!
   255  * Finds a station by preset index
   270  * Finds a station by preset index
   256  */
   271  */
   432  */
   447  */
   433 void RadioStationModel::addStation( const RadioStation& station )
   448 void RadioStationModel::addStation( const RadioStation& station )
   434 {
   449 {
   435     Q_D( RadioStationModel );
   450     Q_D( RadioStationModel );
   436     const int newIndex = findUnusedPresetIndex();
   451     const int newIndex = findUnusedPresetIndex();
   437     LOG_FORMAT( "RadioStationModelPrivate::addStation: Adding station to index %d", newIndex );
   452     LOG_FORMAT( "RadioStationModel::addStation: Adding station to index %d", newIndex );
   438 
   453 
   439     RadioStation newStation = station;
   454     RadioStation newStation = station;
   440     newStation.setPresetIndex( newIndex );
   455     newStation.setPresetIndex( newIndex );
   441     newStation.unsetType( RadioStation::Temporary );
   456     newStation.unsetType( RadioStation::ManualStation );
   442 
   457 
   443     // 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
   444     // the new station will go in the sorted frequency order
   459     // the new station will go in the sorted frequency order
   445     int row = 0;
   460     int row = 0;
   446     const int count = rowCount();
   461     const int count = rowCount();
   456         if ( station.frequency() > existingFreq ) {
   471         if ( station.frequency() > existingFreq ) {
   457             row = 1;
   472             row = 1;
   458         }
   473         }
   459     }
   474     }
   460 
   475 
   461 //    emit layoutAboutToBeChanged();
       
   462     beginInsertRows( QModelIndex(), row, row );
   476     beginInsertRows( QModelIndex(), row, row );
   463 
   477 
   464     d->doSaveStation( newStation );
   478     d->doSaveStation( newStation );
   465 
   479 
   466     d->setCurrentStation( d->mWrapper->currentFrequency() );
   480     d->setCurrentStation( d->mWrapper->currentFrequency() );
   467 
   481 
   468     endInsertRows();
   482     endInsertRows();
   469 
   483 
   470 //    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     }
   471 }
   488 }
   472 
   489 
   473 /*!
   490 /*!
   474  * 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
   475  */
   492  */
   478     Q_D( RadioStationModel );
   495     Q_D( RadioStationModel );
   479     const bool stationHasChanged = station.hasChanged();
   496     const bool stationHasChanged = station.hasChanged();
   480     RadioStation::Change changeFlags = station.changeFlags();
   497     RadioStation::Change changeFlags = station.changeFlags();
   481     station.resetChangeFlags();
   498     station.resetChangeFlags();
   482 
   499 
   483     if ( station.isType( RadioStation::Temporary ) ) {
   500     if ( station.isType( RadioStation::ManualStation ) ) {
   484 
   501 
       
   502         d->mManualStation = station;
   485         emitChangeSignals( station, changeFlags );
   503         emitChangeSignals( station, changeFlags );
   486 
   504 
   487     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   505     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   488 
   506 
   489         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   507         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   508  */
   526  */
   509 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite )
   527 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite )
   510 {
   528 {
   511     Q_D( RadioStationModel );
   529     Q_D( RadioStationModel );
   512     if ( d->mWrapper->isFrequencyValid( frequency ) ) {
   530     if ( d->mWrapper->isFrequencyValid( frequency ) ) {
   513         LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByFrequency, frequency: %d", frequency );
   531         LOG_FORMAT( "RadioStationModel::setFavoriteByFrequency, frequency: %d", frequency );
   514         RadioStation station;
   532         RadioStation station;
   515         if ( findFrequency( frequency, station ) ) { // Update existing preset
   533         if ( findFrequency( frequency, station ) ) { // Update existing preset
   516             if ( station.isFavorite() != favorite ) {
   534             if ( station.isFavorite() != favorite ) {
   517                 station.setFavorite( favorite );
   535                 station.setFavorite( favorite );
   518                 saveStation( station );
   536                 saveStation( station );
   526                 newStation.setFrequency( frequency );
   544                 newStation.setFrequency( frequency );
   527             }
   545             }
   528 
   546 
   529             newStation.setType( RadioStation::LocalStation | RadioStation::Favorite );
   547             newStation.setType( RadioStation::LocalStation | RadioStation::Favorite );
   530 
   548 
   531             // If PI code has been received, it is a local station
       
   532             if ( newStation.hasPiCode() ) {
       
   533                 newStation.setType( RadioStation::LocalStation );
       
   534             }
       
   535 
       
   536             // 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
   537             // 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.
   538             addStation( newStation );
   551             addStation( newStation );
   539         }
   552         }
   540     }
   553     }
   543 /*!
   556 /*!
   544  * Changes the favorite status of a station by its preset index
   557  * Changes the favorite status of a station by its preset index
   545  */
   558  */
   546 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite )
   559 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite )
   547 {
   560 {
   548     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
   561     LOG_FORMAT( "RadioStationModel::setFavoriteByPreset, presetIndex: %d", presetIndex );
   549     RadioStation station;
   562     RadioStation station;
   550     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   563     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   551         station.setFavorite( favorite );
   564         station.setFavorite( favorite );
   552         saveStation( station );
   565         saveStation( station );
   553     }
   566     }
   556 /*!
   569 /*!
   557  * Renames a station by its preset index
   570  * Renames a station by its preset index
   558  */
   571  */
   559 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   572 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   560 {
   573 {
   561     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   574     LOG_FORMAT( "RadioStationModel::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   562     RadioStation station;
   575     RadioStation station;
   563     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   576     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   564         station.setUserDefinedName( name );
   577         station.setUserDefinedName( name );
   565         saveStation( station );
   578         saveStation( station );
   566     }
   579     }
   713     int index = 0;
   726     int index = 0;
   714     for ( ; indexes.contains( index ); ++index ) {
   727     for ( ; indexes.contains( index ); ++index ) {
   715         // Nothing to do here
   728         // Nothing to do here
   716     }
   729     }
   717 
   730 
   718     LOG_FORMAT( "RadioStationModelPrivate::findUnusedPresetIndex, index: %d", index );
   731     LOG_FORMAT( "RadioStationModel::findUnusedPresetIndex, index: %d", index );
   719     return index;
   732     return index;
   720 }
   733 }
   721 
       
   722 /*!
       
   723  * Used by the RDS data setters to find the correct station where the data is set
       
   724  */
       
   725 RadioStation RadioStationModel::findCurrentStation( uint frequency )
       
   726 {
       
   727     Q_D( RadioStationModel );
       
   728     RadioStation station = *d->mCurrentStation;
       
   729     if ( station.frequency() != frequency ) {
       
   730         if ( !findFrequency( frequency, station ) ) {
       
   731             return RadioStation();
       
   732         }
       
   733     }
       
   734     return station;
       
   735 }