radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 32 189d20c34778
parent 28 075425b8d9a4
child 33 11b6825f0862
equal deleted inserted replaced
28:075425b8d9a4 32:189d20c34778
    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  */
   174     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d",
   173     LOG_FORMAT( "RadioStationModelPrivate::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 ) ) {
       
   183             if ( station.isValid() ) {
   181             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( "RadioStationModelPrivate::initialize: Invalid station!" );
   187             }
   185             }
   239 }
   237 }
   240 
   238 
   241 /*!
   239 /*!
   242  * Finds a station by frequency
   240  * Finds a station by frequency
   243  */
   241  */
   244 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) const
   242 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station, FindCriteria::Criteria criteria ) const
   245 {
   243 {
   246     Q_D( const RadioStationModel );
   244     Q_D( const RadioStationModel );
       
   245 
       
   246     if ( criteria == FindCriteria::IncludeManualStation && d->mCurrentStation->frequency() == frequency ) {
       
   247         station = *d->mCurrentStation;
       
   248         return true;
       
   249     }
       
   250 
   247     if ( d->mStations.contains( frequency ) ) {
   251     if ( d->mStations.contains( frequency ) ) {
   248         station = d->mStations.value( frequency );
   252         station = d->mStations.value( frequency );
   249         return true;
   253         return true;
   250     }
   254     }
   251     return false;
   255     return false;
       
   256 }
       
   257 
       
   258 /*!
       
   259  * Convenience function to find a radio station.
       
   260  */
       
   261 RadioStation RadioStationModel::findStation( uint frequency, FindCriteria::Criteria criteria ) const
       
   262 {
       
   263     RadioStation station;
       
   264     findFrequency( frequency, station, criteria ); // Return value ignored
       
   265     return station;
   252 }
   266 }
   253 
   267 
   254 /*!
   268 /*!
   255  * Finds a station by preset index
   269  * Finds a station by preset index
   256  */
   270  */
   436     const int newIndex = findUnusedPresetIndex();
   450     const int newIndex = findUnusedPresetIndex();
   437     LOG_FORMAT( "RadioStationModelPrivate::addStation: Adding station to index %d", newIndex );
   451     LOG_FORMAT( "RadioStationModelPrivate::addStation: Adding station to index %d", newIndex );
   438 
   452 
   439     RadioStation newStation = station;
   453     RadioStation newStation = station;
   440     newStation.setPresetIndex( newIndex );
   454     newStation.setPresetIndex( newIndex );
   441     newStation.unsetType( RadioStation::Temporary );
   455     newStation.unsetType( RadioStation::ManualStation );
   442 
   456 
   443     // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where
   457     // 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
   458     // the new station will go in the sorted frequency order
   445     int row = 0;
   459     int row = 0;
   446     const int count = rowCount();
   460     const int count = rowCount();
   456         if ( station.frequency() > existingFreq ) {
   470         if ( station.frequency() > existingFreq ) {
   457             row = 1;
   471             row = 1;
   458         }
   472         }
   459     }
   473     }
   460 
   474 
   461 //    emit layoutAboutToBeChanged();
       
   462     beginInsertRows( QModelIndex(), row, row );
   475     beginInsertRows( QModelIndex(), row, row );
   463 
   476 
   464     d->doSaveStation( newStation );
   477     d->doSaveStation( newStation );
   465 
   478 
   466     d->setCurrentStation( d->mWrapper->currentFrequency() );
   479     d->setCurrentStation( d->mWrapper->currentFrequency() );
   467 
   480 
   468     endInsertRows();
   481     endInsertRows();
   469 
   482 
   470 //    emit layoutChanged();
   483     // Not all UI components listen to rowsInserted() signal so emit the favorite signal
       
   484     if ( newStation.isFavorite() ) {
       
   485         emit favoriteChanged( *d->mCurrentStation );
       
   486     }
   471 }
   487 }
   472 
   488 
   473 /*!
   489 /*!
   474  * Saves the given station. It is expected to already exist in the list
   490  * Saves the given station. It is expected to already exist in the list
   475  */
   491  */
   478     Q_D( RadioStationModel );
   494     Q_D( RadioStationModel );
   479     const bool stationHasChanged = station.hasChanged();
   495     const bool stationHasChanged = station.hasChanged();
   480     RadioStation::Change changeFlags = station.changeFlags();
   496     RadioStation::Change changeFlags = station.changeFlags();
   481     station.resetChangeFlags();
   497     station.resetChangeFlags();
   482 
   498 
   483     if ( station.isType( RadioStation::Temporary ) ) {
   499     if ( station.isType( RadioStation::ManualStation ) ) {
   484 
   500 
       
   501         d->mManualStation = station;
   485         emitChangeSignals( station, changeFlags );
   502         emitChangeSignals( station, changeFlags );
   486 
   503 
   487     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   504     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   488 
   505 
   489         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   506         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   526                 newStation.setFrequency( frequency );
   543                 newStation.setFrequency( frequency );
   527             }
   544             }
   528 
   545 
   529             newStation.setType( RadioStation::LocalStation | RadioStation::Favorite );
   546             newStation.setType( RadioStation::LocalStation | RadioStation::Favorite );
   530 
   547 
   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
   548             // 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.
   549             // because the UI will probably query the current station in its slots that get called.
   538             addStation( newStation );
   550             addStation( newStation );
   539         }
   551         }
   540     }
   552     }
   716     }
   728     }
   717 
   729 
   718     LOG_FORMAT( "RadioStationModelPrivate::findUnusedPresetIndex, index: %d", index );
   730     LOG_FORMAT( "RadioStationModelPrivate::findUnusedPresetIndex, index: %d", index );
   719     return index;
   731     return index;
   720 }
   732 }
   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 }